dbhelper

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MySQLConnect

func MySQLConnect(dbConfig DbConfig) (db *sqlx.DB, disconnect func(), dbErr error)

MySQLConnect is a function that allows the application to establish a connection with the MySQL database. This function takes a DbConfig structure as an input parameter which includes the configurations required to connect like username, password, host, port and name of the database.

It utilizes the sqlx library's Connect function to create a connection with the MySQL database. If the connection is established successfully, it sets the maximum connection lifetime, maximum number of open connections and maximum idle connections for the MySQL database.

If a connection is not established, the function returns an error. Otherwise, it returns a pointer to the DB and a function to close connection when done.

Usage:

db, disconnect, err := MySQLConnect(cfg)
if err != nil {
    log.Fatalln(err)
}
// Make sure to close the DB connection when you're done using it
defer disconnect()

Parameters:

dbConfig: Specifies the configuration required for connecting to MySQL.

Returns:

*sqlx.DB: Returns a SQLx DB object which can be used to interact with the database.

disconnect: It's a function you can call to close the DB connection.

error: If there is any error encountered during the process, it will be returned as an error type

func MySqlLogDSN added in v0.15.0

func MySqlLogDSN(dbConfig DbConfig) (dsn string)

MySqlLogDSN creates a masked version of the Data Source Name (DSN) for a MySQL database connection. The DSN string is created using the DbConfig object, only it masks the password for security reasons. The returned DSN string includes the username, masked password, host, port, and database name. Two additional parameters are added: a connection timeout of 5 seconds and automatic commit mode enabled. This function is especially useful when logging or displaying the DSN without exposing sensitive information.

The function accepts a single parameter: - dbConfig: a DbConfig object that includes the database username, password, host, port, and database name.

Returns: - dsn: a string representing the masked DSN of the MySQL database connection.

func PgLogDSN added in v0.15.0

func PgLogDSN(dbConfig DbConfig) (dsn string)

PgLogDSN constructs a Postgres Data Source Name (DSN) from the given DbConfig. However, for security reasons, it intentionally obscures the password within the DSN using a placeholder. It returns the DSN as a string.

The DbConfig struct should contain information like Username, Host, Port, and Database name.

The format of the returned DSN string is: "postgres://username:*****@host:port/database"

Usage Example:

cfg := DbConfig{Username: "user1", Host: "localhost", Port: "5432", Database: "mydb"} dsn := PgLogDSN(cfg) fmt.Println(dsn) // Output: "postgres://user1:*****@localhost:5432/mydb"

func PgSQLConnect added in v0.13.0

func PgSQLConnect(dbConfig DbConfig) (db *sqlx.DB, disconnect func(), dbErr error)

PgSQLConnect takes a DbConfig structure as input and establishes a connection to a PostgreSQL database using the details provided in the provided configuration. If successful, the function returns a pointer to the sqlx.DB object representing the database connection, a function that can be called to disconnect from the database, and a nil error. If unsuccessful (e.g., because the connection could not be established or the connection check failed), the function returns a nil pointer, a nil disconnect function, and an error describing the failure.

Types

type DbConfig added in v0.15.0

type DbConfig struct {
	Username string
	Password string
	Host     string
	Port     string
	Database string
}

DbConfig is the configuration for the database

Jump to

Keyboard shortcuts

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