Vasiliy Tolstov 30b5ed1253 rewrite wrapper
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-12-25 15:35:05 +03:00
2021-01-29 22:21:37 +03:00
2022-12-25 15:35:05 +03:00
2022-12-25 15:35:05 +03:00
2022-12-25 15:35:05 +03:00
2021-10-27 18:49:33 +03:00
2022-12-25 15:35:05 +03:00
2022-12-25 15:35:05 +03:00
2021-01-29 22:21:37 +03:00
2022-12-25 15:35:05 +03:00
2022-12-07 10:20:56 +03:00
2022-12-25 15:35:05 +03:00
2022-12-25 15:35:05 +03:00
2021-10-27 18:49:33 +03:00

micro-wrapper-sql

Example for For postgres

package postgres

import (
	"fmt"
	"net/url"
	"time"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/stdlib"
	"github.com/jmoiron/sqlx"
	wrapper "go.unistack.org/micro-wrapper-sql/v3"
)

func Connect(cfg *PostgresConfig) (*sqlx.DB, error) {
	// format connection string
	cstr := fmt.Sprintf(
		"postgres://%s:%s@%s/%s?sslmode=disable&statement_cache_mode=describe",
		cfg.Login,
		url.QueryEscape(cfg.Passw),
		cfg.Addr,
		cfg.DBName,
	)

	// parse connection string
	dbConf, err := pgx.ParseConfig(cstr)
	if err != nil {
		return nil, err
	}

	// needed for pgbouncer
	dbConf.RuntimeParams = map[string]string{
		"standard_conforming_strings": "on",
		"application_name":            cfg.AppName,
	}
	// may be needed for pbbouncer, needs to check
	//dbConf.PreferSimpleProtocol = true
	// register pgx conn
	dsn := stdlib.RegisterConnConfig(dbConf)


  sql.Register("micro-wrapper-sql", wrapper.WrapDriver(
    &stdlib.Driver{}, 
    wrapper.Tracer(some.NewTracer()),
  ))
  wdb, err := sql.Open("micro-wrapper-sql", dsn)
	if err != nil {
		return nil, err
	}

	db := sqlx.NewDb(wdb, "pgx")
	db.SetMaxOpenConns(int(cfg.ConnMax))
	db.SetMaxIdleConns(int(cfg.ConnMaxIdle))
	db.SetConnMaxLifetime(time.Duration(cfg.ConnLifetime) * time.Second)
	db.SetConnMaxIdleTime(time.Duration(cfg.ConnMaxIdleTime) * time.Second)
	
	return db, nil
}
Description
No description provided
Readme
v3.10.15 Latest
2024-12-12 01:57:41 +03:00
Languages
Go 100%