pkg

package
v0.0.0-...-d63fd55 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyConfig

func ApplyConfig(db *sql.DB, config *MySqlDBConfig)

ApplyConfig sets the connection pool settings for a *sql.DB instance using the provided ConnectionConfig.

func BeginPostgres

func BeginPostgres(db *sql.DB) (*sql.Tx, error)

BeginPostgres starts a new transaction. Transactions are useful for ensuring that a series of queries are executed as a single unit of work, and can be rolled back if any of the queries fail.

func CheckDatabase

func CheckDatabase(database *DatabaseConfig, defaults DatabaseConfig)

CheckDatabase checks if the Type, Protocol, Hostname, Port, Name, User, and Password fields in the given ConnectionConfig are set from the DATABASE_TYPE, DATABASE_PROTOCOL, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER, and DATABASE_PASSWORD environment variables. If they are not set, it sets them to the given default values.

func CheckHostname

func CheckHostname(database *DatabaseConfig, defaults string)

CheckHostname checks if the Hostname field in the given ConnectionConfig is set from the DATABASE_HOST environment variable. If it is not set, it sets it to the given default value.

func CheckName

func CheckName(database *DatabaseConfig, defaults string)

CheckName checks if the Name field in the given ConnectionConfig is set from the DATABASE_NAME environment variable. If it is not set, it sets it to the given default value.

func CheckPassword

func CheckPassword(database *DatabaseConfig, defaults string)

CheckPassword checks if the Password field in the given ConnectionConfig is set from the DATABASE_PASSWORD environment variable. If it is not set, it sets it to the given default value.

func CheckPort

func CheckPort(database *DatabaseConfig, defaults string)

CheckPort checks if the Port field in the given ConnectionConfig is set from the DATABASE_PORT environment variable. If it is not set, it sets it to the given default value.

func CheckProtocol

func CheckProtocol(database *DatabaseConfig, defaults string)

CheckProtocol checks if the Protocol field in the given ConnectionConfig is set from the DATABASE_PROTOCOL environment variable. If it is not set, it sets it to the given default value.

func CheckType

func CheckType(database *DatabaseConfig, defaults string)

CheckType checks if the Type field in the given ConnectionConfig is set from the DATABASE_TYPE environment variable. If it is not set, it sets it to the given default value.

func CheckUsername

func CheckUsername(database *DatabaseConfig, defaults string)

CheckUsername checks if the User field in the given ConnectionConfig is set from the DATABASE_USER environment variable. If it is not set, it sets it to the given default value.

func CloseMySQL

func CloseMySQL(db *sql.DB)

func ClosePostgres

func ClosePostgres(db *sql.DB) error

ClosePostgres closes the connection to the Postgres database.

func ConnectMongoDB

func ConnectMongoDB(config MongoDBConfig) (*mongo.Client, error)

ConnectMongoDB establishes a connection to a MongoDB server.

func CreateMySqlTable

func CreateMySqlTable(db *sql.DB, tableName string, s interface{}) error

CreateMySqlTable creates a new table in the database based on the provided struct.

func DisconnectMongoDB

func DisconnectMongoDB(client *mongo.Client) error

DisconnectMongoDB closes the connection to a MongoDB server.

func ExecPostgres

func ExecPostgres(db *sql.DB, query string, args ...interface{}) (sql.Result, error)

ExecPostgres executes a query that does not return rows (e.g. INSERT, UPDATE, DELETE).

func ExecuteSQL

func ExecuteSQL(db *sql.DB, query string, args ...interface{}) (sql.Result, error)

ExecuteSQL is a generic function that can be used to execute any SQL query.

func MapToStruct

func MapToStruct(m map[string]interface{}, s interface{}) error

MapToStruct converts a map[string]interface{} to a struct. The map keys must match the struct field names.

func MongoDBOptions

func MongoDBOptions(config MongoDBConfig) *options.ClientOptions

MongoDBOptions creates a *options.ClientOptions struct using a MongoDBConfig struct.

func MongoDBPing

func MongoDBPing(client *mongo.Client) error

MongoDBPing pings the MongoDB server to test the connection.

func MySqlConn

func MySqlConn(dbType string, dsn string) (*sql.DB, error)

MySqlConn establishes a connection to the database.

func MySqlPingDB

func MySqlPingDB(db *sql.DB) error

MySqlPingDB pings the database to check the connection.

func MySqlString

func MySqlString(config *MySqlDBConfig) string

MySqlString builds connection string for connecting to a MySQL database using the provided ConnectionConfig.

func NewDBConnection

func NewDBConnection(config *MySqlDBConfig) (*sql.DB, error)

NewDBConnection establishes a connection to the MySQL database specified in the provided ConnectionConfig.

func NewRedisClient

func NewRedisClient(config RedisConfig) *redis.Client

NewRedisClient creates a new Redis client using the provided configuration.

func OpenPostgres

func OpenPostgres(connString string) (*sql.DB, error)

OpenPostgres opens a new connection to a Postgres database using the provided connection string.

func PingPostgres

func PingPostgres(db *sql.DB) error

PingPostgres pings the Postgres database to test the connection.

func PostgresString

func PostgresString(config DatabaseConfig) string

PostgresString returns a connection string for a Postgres database.

func QueryPostgres

func QueryPostgres(db *sql.DB, query string, args ...interface{}) (*sql.Rows, error)

QueryPostgres executes a query that returns rows (e.g. SELECT).

func QueryRowPostgres

func QueryRowPostgres(db *sql.DB, query string, args ...interface{}) *sql.Row

QueryRowPostgres executes a query that returns a single row. It is useful when you are expecting a single row as the result of a query (e.g. SELECT COUNT(*) FROM table).

func QuerySQL

func QuerySQL(db *sql.DB, query string, args ...interface{}) ([]map[string]interface{}, error)

QuerySQL is a generic function that can be used to execute any SELECT SQL query. It returns a slice of map[string]interface{}, with each map representing a row in the result set. The keys of the map are the column names, and the values are the column values for that row.

func RedisClose

func RedisClose(client *redis.Client) error

RedisClose closes the connection to the Redis server.

func RedisDelete

func RedisDelete(client *redis.Client, key string) error

RedisDelete deletes a key in Redis.

func RedisExists

func RedisExists(client *redis.Client, key string) (int64, error)

RedisExists checks if a key exists in Redis.

func RedisGet

func RedisGet(client *redis.Client, key string) (interface{}, error)

RedisGet gets the value of a key in Redis.

func RedisHExists

func RedisHExists(client *redis.Client, key string, field string) (bool, error)

RedisHExists checks if a field exists in a hash in Redis.

func RedisHGet

func RedisHGet(client *redis.Client, key string, field string) (interface{}, error)

RedisHGet gets the value of a field in a hash in Redis.

func RedisHSet

func RedisHSet(client *redis.Client, key string, field string, value interface{}) error

RedisHSet sets the value of a field in a hash in Redis.

func RedisOptions

func RedisOptions(config RedisConfig) *redis.Options

RedisOptions creates a *redis.Options struct using a RedisConfig struct.

func RedisPing

func RedisPing(client *redis.Client) error

RedisPing pings the Redis server to test the connection.

func RedisSAdd

func RedisSAdd(client *redis.Client, key string, member interface{}) error

RedisSAdd adds a member to a set in Redis.

func Set

func Set(client *redis.Client, key string, value interface{}) error

Set sets the value of a key in Redis.

func SetupPostgres

func SetupPostgres(config DatabaseConfig) (*sql.DB, error)

SetupPostgres sets up a connection to a Postgres database using the provided configuration.

func WithDBConnection

func WithDBConnection(config *MySqlDBConfig, tableName string, s interface{}, f func(*sql.DB) error) error

WithDBConnection establishes a connection to the database, creates the specified table if it doesn't exist, and then closes the connection when the provided function is finished executing.

Types

type DatabaseConfig

type DatabaseConfig struct {
	Type     string
	Protocol string
	Hostname string
	Port     string
	Name     string
	Username string
	Password string
}

func DefaultMongoDB

func DefaultMongoDB() DatabaseConfig

func DefaultMySql

func DefaultMySql() DatabaseConfig

DefaultMysql returns a DatabaseConfig struct with default values for the fields.

func DefaultPostgres

func DefaultPostgres() DatabaseConfig

func DefaultRedis

func DefaultRedis() DatabaseConfig

func GetDatabase

func GetDatabase(defaults DatabaseConfig) DatabaseConfig

GetDatabase returns a ConnectionConfig struct with the Type, Protocol, Hostname, Port, Name, User, and Password fields set from the DATABASE_TYPE, DATABASE_PROTOCOL, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USER, and DATABASE_PASSWORD environment variables. If they are not set, it sets them to the given default values.

type MongoDBConfig

type MongoDBConfig struct {
	DatabaseConfig
	URI      string
	Database string
}

MongoDBConfig holds the configuration for a MongoDB connection.

func DefaultMongoDBConfig

func DefaultMongoDBConfig() MongoDBConfig

DefaultMongoDBConfig returns a default configuration for a MongoDB connection.

type MySqlDBConfig

type MySqlDBConfig struct {
	DatabaseConfig
	DBName          string
	Timeout         time.Duration
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

MySqlDBConfig is a struct that contains the configuration for the database connection.

func DefaultMySqlConf

func DefaultMySqlConf() *MySqlDBConfig

DefaultMySqlConf returns a struct with default values for the fields.

type RedisConfig

type RedisConfig struct {
	DatabaseConfig
	Address  string
	DB       int
	PoolSize int
}

func DefaultRedisConfig

func DefaultRedisConfig() RedisConfig

DefaultRedisConfig returns a default configuration for a Redis connection.

Jump to

Keyboard shortcuts

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