msql: refactor to use components
This commit is contained in:
parent
f5b0e8e577
commit
b0ee70e585
@ -11,6 +11,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||||
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/merr"
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
||||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||||
@ -20,44 +21,49 @@ import (
|
|||||||
// SQL is a wrapper around a sqlx client which provides more functionality.
|
// SQL is a wrapper around a sqlx client which provides more functionality.
|
||||||
type SQL struct {
|
type SQL struct {
|
||||||
*sqlx.DB
|
*sqlx.DB
|
||||||
ctx context.Context
|
cmp *mcmp.Component
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithMySQL returns a SQL instance which will be initialized and configured
|
// InstMySQL returns a SQL instance which will be initialized when the Init
|
||||||
// when the start event is triggered on the returned Context (see mrun.Start).
|
// event is triggered on the given Component. The SQL instance will have Close
|
||||||
// The SQL instance will have Close called on it when the stop event is
|
// called on it when the Shutdown event is triggered on the given Component.
|
||||||
// triggered on the returned Context (see mrun.Stop).
|
|
||||||
//
|
//
|
||||||
// defaultDB indicates the name of the database in MySQL to use by default,
|
// defaultDB indicates the name of the database in MySQL to use by default,
|
||||||
// though it will be overwritable in the config.
|
// though it will be overwritable in the config.
|
||||||
func WithMySQL(parent context.Context, defaultDB string) (context.Context, *SQL) {
|
func InstMySQL(cmp *mcmp.Component, defaultDB string) *SQL {
|
||||||
ctx := mctx.NewChild(parent, "mysql")
|
sql := SQL{cmp: cmp.Child("mysql")}
|
||||||
sql := new(SQL)
|
|
||||||
|
|
||||||
ctx, addr := mcfg.WithString(ctx, "addr", "[::1]:3306", "Address where mysql server can be found")
|
addr := mcfg.String(sql.cmp, "addr",
|
||||||
ctx, user := mcfg.WithString(ctx, "user", "root", "User to authenticate to mysql server as")
|
mcfg.ParamDefault("[::1]:3306"),
|
||||||
ctx, pass := mcfg.WithString(ctx, "password", "", "Password to authenticate to mysql server with")
|
mcfg.ParamUsage("Address where MySQL server can be found"))
|
||||||
ctx, db := mcfg.WithString(ctx, "database", defaultDB, "mysql database to use")
|
user := mcfg.String(sql.cmp, "user",
|
||||||
ctx = mrun.WithStartHook(ctx, func(innerCtx context.Context) error {
|
mcfg.ParamDefault("root"),
|
||||||
sql.ctx = mctx.Annotate(sql.ctx, "addr", *addr, "user", *user)
|
mcfg.ParamUsage("User to authenticate to MySQL server as"))
|
||||||
|
pass := mcfg.String(sql.cmp, "password",
|
||||||
|
mcfg.ParamUsage("Password to authenticate to MySQL server with"))
|
||||||
|
db := mcfg.String(sql.cmp, "database",
|
||||||
|
mcfg.ParamDefault(defaultDB),
|
||||||
|
mcfg.ParamUsage("MySQL database to use"))
|
||||||
|
|
||||||
|
mrun.InitHook(sql.cmp, func(ctx context.Context) error {
|
||||||
|
sql.cmp.Annotate("addr", *addr, "user", *user)
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", *user, *pass, *addr, *db)
|
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", *user, *pass, *addr, *db)
|
||||||
mlog.Debug("constructed dsn", mctx.Annotate(sql.ctx, "dsn", dsn))
|
mlog.From(sql.cmp).Debug("constructed dsn", mctx.Annotate(ctx, "dsn", dsn))
|
||||||
mlog.Info("connecting to mysql server", sql.ctx)
|
mlog.From(sql.cmp).Info("connecting to MySQL server", ctx)
|
||||||
var err error
|
var err error
|
||||||
sql.DB, err = sqlx.ConnectContext(innerCtx, "mysql", dsn)
|
sql.DB, err = sqlx.ConnectContext(ctx, "mysql", dsn)
|
||||||
return merr.Wrap(err, sql.ctx)
|
return merr.Wrap(err, sql.cmp.Context(), ctx)
|
||||||
})
|
|
||||||
ctx = mrun.WithStopHook(ctx, func(innerCtx context.Context) error {
|
|
||||||
mlog.Info("closing connection to sql server", sql.ctx)
|
|
||||||
return sql.Close()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
sql.ctx = ctx
|
mrun.ShutdownHook(sql.cmp, func(ctx context.Context) error {
|
||||||
return mctx.WithChild(parent, ctx), sql
|
mlog.From(sql.cmp).Info("closing connection to MySQL server", ctx)
|
||||||
|
return merr.Wrap(sql.Close(), sql.cmp.Context(), ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
return &sql
|
||||||
}
|
}
|
||||||
|
|
||||||
// Context returns the annotated Context from this instance's initialization.
|
// Context returns the annotated Context from this instance's initialization.
|
||||||
func (sql *SQL) Context() context.Context {
|
func (sql *SQL) Context() context.Context {
|
||||||
return sql.Context()
|
return sql.cmp.Context()
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMySQL(t *T) {
|
func TestMySQL(t *T) {
|
||||||
ctx := mtest.Context()
|
cmp := mtest.Component()
|
||||||
ctx, sql := WithMySQL(ctx, "test")
|
sql := InstMySQL(cmp, "test")
|
||||||
mtest.Run(ctx, t, func() {
|
mtest.Run(cmp, t, func() {
|
||||||
_, err := sql.Exec("CREATE TABLE IF NOT EXISTS msql_test (id INT);")
|
_, err := sql.Exec("CREATE TABLE IF NOT EXISTS msql_test (id INT);")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user