sqlxopt

package module
v0.0.0-...-a98e665 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 5 Imported by: 0

README

sqlx-option

more convenient to init golang-sqlx db

Feature:
  • init sqlx db by using Option type:
type Option struct {
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Host     string `mapstructure:"host"`
	Port     uint32 `mapstructure:"port"`
	Charset  string `mapstructure:"charset"`
	DbName   string `mapstructure:"dbname"`

	Driver   string `mapstructure:"driver"`  // sql driver for sqlx, the default is `mysql`
}
  • init sqlx db by using yaml config file, format: the key is mysql.dev
mysql:
  dev:
    user: "root"
    password: ""
    host: "127.0.0.1"
    port: "3306"
    charset: "utf8mb4"
    dbname: eth-store
Examples:

init sqlx db by using Option type:

    opt := &Option{
        User: "root",
        Password: "",
        Host: "127.0.0.1",
        Port: 3306,
        Charset: "utf8mb4",
        DbName: "sqlx",
    }
    db, err := sqlxopt.NewDBWithOption(opt)

init sqlx db by using yaml config file, the key is mysql.dev

 
db, err := sqlxopt.NewDBWithConfigFile("config.yaml", "mysql.dev")

config.yaml:

mysql:
  dev:
    User: "root"
    Password: ""
    Host: "127.0.0.1"
    Port: 3306
    Charset: "utf8mb4"
    DbName: "sqlx"
Modules:

two modules must be in the $GOPATH:

go get "github.com/jmoiron/sqlx"
go get "github.com/spf13/viper"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDB

func NewDB(driver, source string) (*sqlx.DB, error)

NewDB creates sqlx.DB by using `source` string and driver

func NewDBWithConfigFile

func NewDBWithConfigFile(configFile string, key string) (*sqlx.DB, error)

NewDBWithConfig creates sqlx.DB by using yaml config file

func NewDBWithOption

func NewDBWithOption(opt Option) (*sqlx.DB, error)

NewDBWithOption creates sqlx.DB by using `Option`

Types

type Option

type Option struct {
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	Host     string `mapstructure:"host"`
	Port     uint32 `mapstructure:"port"`
	Charset  string `mapstructure:"charset"`
	DbName   string `mapstructure:"dbname"`

	Driver string `mapstructure:"driver"` // sql driver for sqlx, the default is `mysql`
}

func LoadOptionFromConfigFile

func LoadOptionFromConfigFile(configFile string, key string) (opt Option, err error)

func (Option) String

func (opt Option) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL