Go to file
2022-05-03 21:15:26 +03:00
.github Bump github/codeql-action from 1 to 2 (#37) 2022-04-25 21:44:59 +03:00
.gitignore Initial commit 2021-01-29 22:21:37 +03:00
common.go update workflows 2021-10-27 18:49:33 +03:00
conn.go update workflows 2021-10-27 18:49:33 +03:00
doc.go update workflows 2021-10-27 18:49:33 +03:00
driver.go update workflows 2021-10-27 18:49:33 +03:00
go.mod Bump go.unistack.org/micro/v3 from 3.9.7 to 3.9.8 2022-05-03 18:13:35 +00:00
go.sum Bump go.unistack.org/micro/v3 from 3.9.7 to 3.9.8 2022-05-03 18:13:35 +00:00
LICENSE Initial commit 2021-01-29 22:21:37 +03:00
options.go update workflows 2021-10-27 18:49:33 +03:00
README.md update workflows 2021-10-27 18:49:33 +03:00
stmt.go update workflows 2021-10-27 18:49:33 +03:00
tx.go update workflows 2021-10-27 18:49:33 +03:00
wrapper.go update workflows 2021-10-27 18:49:33 +03:00

micro-wrapper-sqlpackage postgres

Example for For postgres

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

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/stdlib"
	"github.com/jmoiron/sqlx"
)

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
}