func (dbs *DBs) GetContextOnMaster(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _get(ctx, dbs.masters, dest, query, args...)
	return
}
GetContextOnMaster on masters.
Any placeholder parameters are replaced with supplied args.
An error is returned if the result set is empty.
package mssqlx
import (
	"context"
	"database/sql"
	"errors"
	"os"
	"sync"
	"time"
	"github.com/hashicorp/go-multierror"
	"github.com/jmoiron/sqlx"
)
var (
	ErrNoConnection = errors.New("no connection available")
	ErrNoConnectionOrWsrep = errors.New("no connection available or Wsrep is not ready")
)
const (
	DefaultHealthCheckPeriodInMilli = 40
)
var hostName string
func init() {
	hostName, _ = os.Hostname()
}
func ping(w *wrapper) (err error) {
	_, err = w.db.Exec("SELECT 1")
	return
}
type DBs struct {
	driverName      string
	readQuerySource ReadQuerySource
	masters *balancer
	slaves  *balancer
	all     *balancer
	_masters []*wrapper
	_slaves  []*wrapper
	_all     []*wrapper
}
func (dbs *DBs) DriverName() string {
	return dbs.driverName
}
func (dbs *DBs) getDBs(s []*wrapper) ([]*sqlx.DB, int) {
	n := len(s)
	r := make([]*sqlx.DB, n)
	for i, v := range s {
		r[i] = v.db
	}
	return r, n
}
func (dbs *DBs) readBalancer() *balancer {
	if dbs.readQuerySource == ReadQuerySourceAll {
		return dbs.all
	}
	return dbs.slaves
}
func (dbs *DBs) GetAllMasters() ([]*sqlx.DB, int) {
	return dbs.getDBs(dbs._masters)
}
func (dbs *DBs) GetAllSlaves() ([]*sqlx.DB, int) {
	return dbs.getDBs(dbs._slaves)
}
func _ping(target []*wrapper) []error {
	if target == nil {
		return nil
	}
	nn := len(target)
	if nn == 0 {
		return nil
	}
	errResult := make([]error, nn)
	var wg sync.WaitGroup
	for i := range target {
		if target[i] != nil && target[i].db != nil {
			wg.Add(1)
			go func(ind int, wg *sync.WaitGroup) {
				errResult[ind] = target[ind].db.Ping()
				wg.Done()
			}(i, &wg)
		}
	}
	wg.Wait()
	return errResult
}
func (dbs *DBs) Ping() []error {
	return _ping(dbs._all)
}
func (dbs *DBs) PingMaster() []error {
	return _ping(dbs._masters)
}
func (dbs *DBs) PingSlave() []error {
	return _ping(dbs._slaves)
}
func _close(target []*wrapper) []error {
	if target == nil {
		return nil
	}
	nn := len(target)
	if nn == 0 {
		return nil
	}
	errResult := make([]error, nn)
	var wg sync.WaitGroup
	for i, db := range target {
		if db != nil && db.db != nil {
			wg.Add(1)
			go func(db *wrapper, ind int, wg *sync.WaitGroup) {
				errResult[ind] = db.db.Close()
				wg.Done()
			}(db, i, &wg)
		}
	}
	wg.Wait()
	return errResult
}
func (dbs *DBs) Destroy() []error {
	res := _close(dbs._all)
	if dbs.masters != nil {
		dbs.masters.destroy()
	}
	if dbs.slaves != nil {
		dbs.slaves.destroy()
	}
	dbs.all.destroy()
	return res
}
func (dbs *DBs) DestroyMaster() []error {
	if dbs.masters != nil {
		dbs.masters.destroy()
	}
	return _close(dbs._masters)
}
func (dbs *DBs) DestroySlave() []error {
	if dbs.slaves != nil {
		dbs.slaves.destroy()
	}
	return _close(dbs._slaves)
}
func _setMaxIdleConns(target []*wrapper, n int) {
	if target == nil {
		return
	}
	nn := len(target)
	if nn == 0 {
		return
	}
	var wg sync.WaitGroup
	for _, db := range target {
		if db != nil && db.db != nil {
			wg.Add(1)
			go func(db *wrapper, wg *sync.WaitGroup) {
				db.db.SetMaxIdleConns(n)
				wg.Done()
			}(db, &wg)
		}
	}
	wg.Wait()
}
func (dbs *DBs) SetHealthCheckPeriod(period uint64) {
	dbs.masters.setHealthCheckPeriod(period)
	dbs.slaves.setHealthCheckPeriod(period)
}
func (dbs *DBs) SetMasterHealthCheckPeriod(period uint64) {
	dbs.masters.setHealthCheckPeriod(period)
}
func (dbs *DBs) SetSlaveHealthCheckPeriod(period uint64) {
	dbs.slaves.setHealthCheckPeriod(period)
}
func (dbs *DBs) SetMaxIdleConns(n int) {
	_setMaxIdleConns(dbs._all, n)
}
func (dbs *DBs) SetMasterMaxIdleConns(n int) {
	_setMaxIdleConns(dbs._masters, n)
}
func (dbs *DBs) SetSlaveMaxIdleConns(n int) {
	_setMaxIdleConns(dbs._slaves, n)
}
func _setMaxOpenConns(target []*wrapper, n int) {
	if target == nil {
		return
	}
	nn := len(target)
	if nn == 0 {
		return
	}
	var wg sync.WaitGroup
	for _, db := range target {
		if db != nil && db.db != nil {
			wg.Add(1)
			go func(db *wrapper, wg *sync.WaitGroup) {
				db.db.SetMaxOpenConns(n)
				wg.Done()
			}(db, &wg)
		}
	}
	wg.Wait()
}
func (dbs *DBs) SetMaxOpenConns(n int) {
	_setMaxOpenConns(dbs._all, n)
}
func (dbs *DBs) SetMasterMaxOpenConns(n int) {
	_setMaxOpenConns(dbs._masters, n)
}
func (dbs *DBs) SetSlaveMaxOpenConns(n int) {
	_setMaxOpenConns(dbs._slaves, n)
}
func _setConnMaxLifetime(target []*wrapper, d time.Duration) {
	if target == nil {
		return
	}
	nn := len(target)
	if nn == 0 {
		return
	}
	var wg sync.WaitGroup
	for _, db := range target {
		if db != nil && db.db != nil {
			wg.Add(1)
			go func(db *wrapper, wg *sync.WaitGroup) {
				db.db.SetConnMaxLifetime(d)
				wg.Done()
			}(db, &wg)
		}
	}
	wg.Wait()
}
func (dbs *DBs) SetConnMaxLifetime(d time.Duration) {
	_setConnMaxLifetime(dbs._all, d)
}
func (dbs *DBs) SetMasterConnMaxLifetime(d time.Duration) {
	_setConnMaxLifetime(dbs._masters, d)
}
func (dbs *DBs) SetSlaveConnMaxLifetime(d time.Duration) {
	_setConnMaxLifetime(dbs._slaves, d)
}
func _stats(target []*wrapper) []sql.DBStats {
	if target == nil {
		return nil
	}
	nn := len(target)
	if nn == 0 {
		return nil
	}
	result := make([]sql.DBStats, nn)
	var wg sync.WaitGroup
	for ind, db := range target {
		if db != nil && db.db != nil {
			wg.Add(1)
			go func(db *wrapper, ind int, wg *sync.WaitGroup) {
				result[ind] = db.db.Stats()
				wg.Done()
			}(db, ind, &wg)
		}
	}
	wg.Wait()
	return result
}
func (dbs *DBs) Stats() (stats []sql.DBStats) {
	stats = _stats(dbs._all)
	return
}
func (dbs *DBs) StatsMaster() (stats []sql.DBStats) {
	stats = _stats(dbs._masters)
	return
}
func (dbs *DBs) StatsSlave() (stats []sql.DBStats) {
	stats = _stats(dbs._slaves)
	return
}
func _mapperFunc(target []*wrapper, mf func(string) string) {
	if target == nil {
		return
	}
	nn := len(target)
	if nn == 0 {
		return
	}
	var wg sync.WaitGroup
	for ind, db := range target {
		if db != nil {
			wg.Add(1)
			go func(db *wrapper, ind int) {
				db.db.MapperFunc(mf)
				wg.Done()
			}(db, ind)
		}
	}
	wg.Wait()
}
func (dbs *DBs) MapperFunc(mf func(string) string) {
	_mapperFunc(dbs._all, mf)
}
func (dbs *DBs) MapperFuncMaster(mf func(string) string) {
	_mapperFunc(dbs._masters, mf)
}
func (dbs *DBs) MapperFuncSlave(mf func(string) string) {
	_mapperFunc(dbs._slaves, mf)
}
func (dbs *DBs) Rebind(query string) string {
	if dbs._all == nil || len(dbs._all) == 0 {
		return ""
	}
	for _, db := range dbs._all {
		if db != nil && db.db != nil {
			return db.db.Rebind(query)
		}
	}
	return ""
}
func (dbs *DBs) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
	if dbs._all == nil || len(dbs._all) == 0 {
		return "", nil, ErrNoConnection
	}
	for _, db := range dbs._all {
		if db != nil {
			return db.db.BindNamed(query, arg)
		}
	}
	return "", nil, ErrNoConnection
}
func getDBFromBalancer(target *balancer) (db *wrapper, err error) {
	if db = target.get(target.isMulti); db != nil {
		return
	}
	for i := 0; i < 3; i++ {
		time.Sleep(time.Duration(target.getHealthCheckPeriod()) * time.Millisecond)
		if db = target.get(target.isMulti); db != nil {
			return
		}
	}
	if target.isWsrep {
		err = ErrNoConnectionOrWsrep
	} else {
		err = ErrNoConnection
	}
	return
}
func shouldFailure(w *wrapper, isWsrep bool, err error) (bool, error) {
	if err == nil || isStdErr(err) {
		return false, err
	}
	if isWsrep && IsWsrepNotReady(err) {
		return true, err
	}
	if e := ping(w); e != nil {
		err = multierror.Append(err, e).ErrorOrNil()
		return true, err
	}
	return false, err
}
func _namedQuery(ctx context.Context, target *balancer, query string, arg interface{}) (res *sqlx.Rows, err error) {
	var (
		w *wrapper
		r interface{}
	)
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		r, err = retryFunc(query, func() (interface{}, error) {
			return w.db.NamedQueryContext(ctx, query, arg)
		})
		if r != nil {
			res = r.(*sqlx.Rows)
		}
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		return
	}
}
func (dbs *DBs) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error) {
	return _namedQuery(context.Background(), dbs.readBalancer(), query, arg)
}
func (dbs *DBs) NamedQueryOnMaster(query string, arg interface{}) (*sqlx.Rows, error) {
	return _namedQuery(context.Background(), dbs.masters, query, arg)
}
func (dbs *DBs) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error) {
	return _namedQuery(ctx, dbs.readBalancer(), query, arg)
}
func (dbs *DBs) NamedQueryContextOnMaster(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error) {
	return _namedQuery(ctx, dbs.masters, query, arg)
}
func _namedExec(ctx context.Context, target *balancer, query string, arg interface{}) (res sql.Result, err error) {
	var (
		w *wrapper
		r interface{}
	)
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		r, err = retryFunc(query, func() (interface{}, error) {
			return w.db.NamedExecContext(ctx, query, arg)
		})
		if r != nil {
			res = r.(sql.Result)
		}
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		return
	}
}
func (dbs *DBs) NamedExec(query string, arg interface{}) (sql.Result, error) {
	return _namedExec(context.Background(), dbs.masters, query, arg)
}
func (dbs *DBs) NamedExecOnSlave(query string, arg interface{}) (sql.Result, error) {
	return _namedExec(context.Background(), dbs.slaves, query, arg)
}
func (dbs *DBs) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
	return _namedExec(ctx, dbs.masters, query, arg)
}
func (dbs *DBs) NamedExecContextOnSlave(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
	return _namedExec(ctx, dbs.slaves, query, arg)
}
func _query(ctx context.Context, target *balancer, query string, args ...interface{}) (dbr *wrapper, res *sql.Rows, err error) {
	var (
		w *wrapper
		r interface{}
	)
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		r, err = retryFunc(query, func() (interface{}, error) {
			return w.db.QueryContext(ctx, query, args...)
		})
		if r != nil {
			res = r.(*sql.Rows)
		}
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		dbr = w
		return
	}
}
func (dbs *DBs) Query(query string, args ...interface{}) (r *sql.Rows, err error) {
	_, r, err = _query(context.Background(), dbs.readBalancer(), query, args...)
	return
}
func (dbs *DBs) QueryOnMaster(query string, args ...interface{}) (r *sql.Rows, err error) {
	_, r, err = _query(context.Background(), dbs.masters, query, args...)
	return
}
func (dbs *DBs) QueryContext(ctx context.Context, query string, args ...interface{}) (r *sql.Rows, err error) {
	_, r, err = _query(ctx, dbs.readBalancer(), query, args...)
	return
}
func (dbs *DBs) QueryContextOnMaster(ctx context.Context, query string, args ...interface{}) (r *sql.Rows, err error) {
	_, r, err = _query(ctx, dbs.masters, query, args...)
	return
}
func _queryx(ctx context.Context, target *balancer, query string, args ...interface{}) (dbr *wrapper, res *sqlx.Rows, err error) {
	var (
		w *wrapper
		r interface{}
	)
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		r, err = retryFunc(query, func() (interface{}, error) {
			return w.db.QueryxContext(ctx, query, args...)
		})
		if r != nil {
			res = r.(*sqlx.Rows)
		}
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		dbr = w
		return
	}
}
func (dbs *DBs) Queryx(query string, args ...interface{}) (r *sqlx.Rows, err error) {
	_, r, err = _queryx(context.Background(), dbs.readBalancer(), query, args...)
	return
}
func (dbs *DBs) QueryxOnMaster(query string, args ...interface{}) (r *sqlx.Rows, err error) {
	_, r, err = _queryx(context.Background(), dbs.masters, query, args...)
	return
}
func (dbs *DBs) QueryxContext(ctx context.Context, query string, args ...interface{}) (r *sqlx.Rows, err error) {
	_, r, err = _queryx(ctx, dbs.readBalancer(), query, args...)
	return
}
func (dbs *DBs) QueryxContextOnMaster(ctx context.Context, query string, args ...interface{}) (r *sqlx.Rows, err error) {
	_, r, err = _queryx(ctx, dbs.masters, query, args...)
	return
}
func (dbs *DBs) _queryRow(ctx context.Context, target *balancer, query string, args ...interface{}) *sql.Row {
	w, err := getDBFromBalancer(target)
	if err != nil {
		w = dbs._all[0]
	}
	return w.db.QueryRowContext(ctx, query, args...)
}
func (dbs *DBs) QueryRow(query string, args ...interface{}) *sql.Row {
	return dbs._queryRow(context.Background(), dbs.readBalancer(), query, args...)
}
func (dbs *DBs) QueryRowOnMaster(query string, args ...interface{}) *sql.Row {
	return dbs._queryRow(context.Background(), dbs.masters, query, args...)
}
func (dbs *DBs) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
	return dbs._queryRow(ctx, dbs.readBalancer(), query, args...)
}
func (dbs *DBs) QueryRowContextOnMaster(ctx context.Context, query string, args ...interface{}) *sql.Row {
	return dbs._queryRow(ctx, dbs.masters, query, args...)
}
func (dbs *DBs) _queryRowx(ctx context.Context, target *balancer, query string, args ...interface{}) *sqlx.Row {
	w, err := getDBFromBalancer(target)
	if err != nil {
		w = dbs._all[0]
	}
	return w.db.QueryRowxContext(ctx, query, args...)
}
func (dbs *DBs) QueryRowx(query string, args ...interface{}) *sqlx.Row {
	return dbs._queryRowx(context.Background(), dbs.readBalancer(), query, args...)
}
func (dbs *DBs) QueryRowxOnMaster(query string, args ...interface{}) *sqlx.Row {
	return dbs._queryRowx(context.Background(), dbs.masters, query, args...)
}
func (dbs *DBs) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
	return dbs._queryRowx(ctx, dbs.readBalancer(), query, args...)
}
func (dbs *DBs) QueryRowxContextOnMaster(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
	return dbs._queryRowx(ctx, dbs.masters, query, args...)
}
func _select(ctx context.Context, target *balancer, dest interface{}, query string, args ...interface{}) (dbr *wrapper, err error) {
	var w *wrapper
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		_, err = retryFunc(query, func() (interface{}, error) {
			return nil, w.db.SelectContext(ctx, dest, query, args...)
		})
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		dbr = w
		return
	}
}
func (dbs *DBs) Select(dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _select(context.Background(), dbs.readBalancer(), dest, query, args...)
	return
}
func (dbs *DBs) SelectOnMaster(dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _select(context.Background(), dbs.masters, dest, query, args...)
	return
}
func (dbs *DBs) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _select(ctx, dbs.readBalancer(), dest, query, args...)
	return
}
func (dbs *DBs) SelectContextOnMaster(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _select(ctx, dbs.masters, dest, query, args...)
	return
}
func _get(ctx context.Context, target *balancer, dest interface{}, query string, args ...interface{}) (dbr *wrapper, err error) {
	var w *wrapper
	for {
		if w, err = getDBFromBalancer(target); err != nil {
			reportError(query, err)
			return
		}
		_, err = retryFunc(query, func() (interface{}, error) {
			return nil, w.db.GetContext(ctx, dest, query, args...)
		})
		var should bool
		if should, err = shouldFailure(w, target.isWsrep, err); should {
			target.failure(w, err)
			continue
		}
		dbr = w
		return
	}
}
func (dbs *DBs) Get(dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _get(context.Background(), dbs.readBalancer(), dest, query, args...)
	return
}
func (dbs *DBs) GetOnMaster(dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _get(context.Background(), dbs.masters, dest, query, args...)
	return
}
func (dbs *DBs) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
	_, err = _get(ctx, dbs.readBalancer(), dest, query, args...)
	return
}