Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthChecker ¶
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 ¶
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)
}
Output:
func NewMySQL ¶
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)
}
Output:
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)
}
Output: