mysql

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() *sql.DB

Default returns the default *sql.DB instance created by InitDefault.

func HealthChecker

func HealthChecker() health.Checker

HealthChecker returns a health.Checker that verifies MySQL connectivity by pinging the default connection. The returned Checker reports health.StatusUnhealthy when no connection has been initialised or when the ping fails; health.StatusHealthy when reachable.

Example

ExampleHealthChecker shows how to wire HealthChecker into a health.Check call for a /health HTTP endpoint. In test environments with no reachable MySQL server, the Checker reports StatusUnhealthy regardless of whether the singleton has been initialised.

package main

import (
	"context"
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/mysql"
	"github.com/phcp-tech/common-library-golang/health"
)

func main() {
	results := health.Check(context.Background(), mysql.HealthChecker())
	fmt.Println(results[0].Name)
	fmt.Println(results[0].Status == health.StatusUnhealthy) // true — no reachable MySQL
}
Output:
database
true

func InitDefault

func InitDefault(conf *Config) error

InitDefault initializes the default singleton MySQL connection.

Example

ExampleInitDefault shows the singleton pattern: call InitDefault once at application startup. Subsequent calls are silently ignored (sync.Once). After a successful call, Default returns the initialised *sql.DB which can be passed directly to sqlc-generated Queries.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/mysql"
)

func main() {
	err := mysql.InitDefault(&mysql.Config{
		Host:     "127.0.0.1",
		Port:     "3306",
		Database: "mydb",
		Username: "user",
		Password: "pass",
	})
	fmt.Println(err == nil)
	fmt.Println(mysql.Default() != nil)
}

func NewMySQL

func NewMySQL(conf *Config) (*sql.DB, error)

NewMySQL opens a *sql.DB connection to MySQL and configures the connection pool. It returns the raw standard-library handle so sqlc-generated Queries can use it directly.

Example

ExampleNewMySQL shows how to open a MySQL connection with the default pool settings. sql.Open is lazy — no connection is established until the database is first used. For cases that require multiple connections use NewMySQL directly instead of the singleton.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/mysql"
)

func main() {
	db, err := mysql.NewMySQL(&mysql.Config{
		Host:     "127.0.0.1",
		Port:     "3306",
		Database: "mydb",
		Username: "user",
		Password: "pass",
	})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer db.Close()
	fmt.Println(db != nil)
}
Example (CustomPool)

ExampleNewMySQL_customPool shows how to override the default connection pool settings. Zero-value pool fields fall back to the dbsqlc package defaults (MaxOpenConns=100, MaxIdleConns=25, ConnMaxLifetime=60min, ConnMaxIdletime=10min).

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/mysql"
)

func main() {
	db, err := mysql.NewMySQL(&mysql.Config{
		Host:            "127.0.0.1",
		Port:            "3306",
		Database:        "mydb",
		Username:        "user",
		Password:        "pass",
		MaxOpenConns:    50,
		MaxIdleConns:    10,
		ConnMaxLifetime: 30, // minutes
		ConnMaxIdletime: 5,  // minutes
	})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer db.Close()
	fmt.Println(db != nil)
}

Types

type Config

type Config struct {
	// Database connection parameters
	Host     string
	Port     string
	Database string
	Username string
	Password string

	// Connection pool parameters
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	ConnMaxIdletime int
}

Directories

Path Synopsis
Package component provides MySQL lifecycle integration for bootstrap.
Package component provides MySQL lifecycle integration for bootstrap.

Jump to

Keyboard shortcuts

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