dbconnector

package module
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2022 License: MIT Imports: 18 Imported by: 0

README

Readme

优先级

如果存在连接器文件,比如 xxx.contr,那么模块会先行解析并加载。如果配置文件中存在配置信息部分(明文结构体),模块会继续分析,并覆盖相同key的连接信息。如果是在开发模式下,方便明文配置数据库连接,则可以在配置文件中保留配置信息部分,作为正式发行版本,应该删除该配置数据,只保留连接器文件即可

install

go get github.com/tinybear1976/dbconnector

basic

  1. load config
import github.com/tinybear1976/dbconnector

// ...
currentPath:=os.Getwd() // or get from Args
// Automatically scan files(.contr) in a directory and load
dbconnector.AddFromFiles(currentPath)
// (if there is) get from config file. if the keys are the same, the latter overrides the former
dbconnector.AddFromStructs(mariadbSlice,redisSlice) 
  1. save contr file (reference)
m := Mariadb_t{
		Key:     "test",
		Server:  "127.0.0.1",
		Port:    3306,
		Uid:     "root",
		Pwd:     "1234#@&!Keen",
		DB:      "city",
		Timeout:      "",
		ReadTimeout:  "",
		WriteTimeout: "",
	}
// only main file name
err := m.SaveConnectorFile("t1")

m := Redis_t{
		Key:    "local",
		Server: "127.0.0.1",
		Port:   6379,
		Pwd:    "",
		DB:     0,
	}
main_name := "redis_local"
err := m.SaveConnectorFile(main_name)
  1. use in project mariadb:
// ...
const (
    // Mariadb_t{ key:"app",.... }
    APP MariadbConnectors="app"
)

// ...
func function()  {
    //...
    // ok: return *sqlx.DB, else: return nil
    db:=APP.Connector()
    // db.Query... 
    //...
}

redis

// ...
const (
    // Redis_t{ key:"redislocal",.... }
    REDISLOCAL RedisConnectors="redislocal"
)

// ...
func function()  {
    //...
    // ok: return *redis.Conn,nil, else: return nil,error
    conn,err:=REDISLOCAL.Connect()
    // (*conn).Do()
    // conn.GET()
    //...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFromFiles

func AddFromFiles(currentPath string) error

根据指定路径搜索所有连接器文件,并进行解析,自动添加

func AddFromStructs

func AddFromStructs(ms []Mariadb_t, rs []Redis_t, ps []Postgres_t) (err error)

从连接配置结构体切片中进行添加,一般改函数用于第二步,从配置文件中读取后,进行添加,如果与之前的文件连接器key冲突,这里将覆盖之前的内容

func AddOnlyMariadb added in v1.0.10

func AddOnlyMariadb(ms []Mariadb_t) (err error)

只增加Mariadb连接

func AddOnlyPostgres added in v1.0.10

func AddOnlyPostgres(ps []Postgres_t) (err error)

只增加Postgres连接

func AddOnlyRedis added in v1.0.10

func AddOnlyRedis(rs []Redis_t) (err error)

只增加Redis连接

func BGREWRITEAOF added in v1.0.1

func BGREWRITEAOF(conn *redis.Conn) error

func CleanMariadb

func CleanMariadb()

清除所有的连接器

func CleanPostgres added in v1.0.9

func CleanPostgres()

清除所有的连接器

func CleanRedis

func CleanRedis()

清除所有的连接器

func DEL added in v1.0.1

func DEL(conn *redis.Conn, keys ...interface{}) (err error)

func DecryptConnectorFile

func DecryptConnectorFile(filename string) (string, error)

解密一个指定的连接器文件,只还原字符串原型,不做进一步解析

func Diconnect added in v1.0.1

func Diconnect(conn *redis.Conn) (err error)

断开连接

func EXISTS added in v1.0.1

func EXISTS(conn *redis.Conn, key string) (ret bool, err error)

func GET added in v1.0.1

func GET(conn *redis.Conn, key string) (string, error)

常用命令GET

func HDEL added in v1.0.1

func HDEL(conn *redis.Conn, params ...interface{}) (err error)

func HGETALL added in v1.0.1

func HGETALL(conn *redis.Conn, key string) (ret map[string]string, err error)

func HMGET added in v1.0.1

func HMGET(conn *redis.Conn, params ...interface{}) (vals []string, err error)

func HMSET added in v1.0.1

func HMSET(conn *redis.Conn, params ...interface{}) (err error)

func KEYS added in v1.0.1

func KEYS(conn *redis.Conn, query string) (keys []string, err error)

func SET added in v1.0.1

func SET(conn *redis.Conn, key, value string) (err error)

Types

type MariadbConnector added in v1.0.9

type MariadbConnector string

func (MariadbConnector) Connector added in v1.0.9

func (ctr MariadbConnector) Connector() *sqlx.DB

返回连接器,如果没有找到则连接器返回nil

func (MariadbConnector) Info added in v1.0.9

func (ctr MariadbConnector) Info() *Mariadb_t

返回连接信息,如果没有找到则连接器返回nil

type Mariadb_t

type Mariadb_t struct {
	Key          string `toml:"key" json:"key"`
	Server       string `toml:"server" json:"server"`
	Port         int    `toml:"port" json:"port"`
	Uid          string `toml:"uid" json:"uid"`
	Pwd          string `toml:"pwd" json:"pwd"`
	DB           string `toml:"db" json:"db"`
	Timeout      string `toml:"timeout" json:"timeout"`           // 字符串,携带单位如果忘记写默认s
	ReadTimeout  string `toml:"readTimeout" json:"readTimeout"`   // 字符串,携带单位如果忘记写默认s
	WriteTimeout string `toml:"writeTimeout" json:"writeTimeout"` // 字符串,携带单位如果忘记写默认s
}

func (Mariadb_t) CreateConnector

func (t Mariadb_t) CreateConnector(output io.Writer) error

将连接信息按照规范写入流(加密后内容)

func (Mariadb_t) SaveConnectorFile

func (t Mariadb_t) SaveConnectorFile(onlyMainFilename string) error

提供文件名(不含后缀,模块自动添加),保存生成连接器文件

type PostgresConnector added in v1.0.9

type PostgresConnector string

func (PostgresConnector) Connector added in v1.0.9

func (ctr PostgresConnector) Connector() *sqlx.DB

返回连接器,如果没有找到则连接器返回nil

func (PostgresConnector) Info added in v1.0.9

func (ctr PostgresConnector) Info() *Postgres_t

返回连接信息,如果没有找到则连接器返回nil

type Postgres_t added in v1.0.9

type Postgres_t struct {
	Key      string `toml:"key" json:"key"`
	Server   string `toml:"server" json:"server"`
	Port     int    `toml:"port" json:"port"`
	Username string `toml:"user" json:"user"`
	Pwd      string `toml:"pwd" json:"pwd"`
	DB       string `toml:"db" json:"db"`
	Timeout  int    `toml:"timeout" json:"timeout"`
}

func (Postgres_t) CreateConnector added in v1.0.9

func (t Postgres_t) CreateConnector(output io.Writer) error

将连接信息按照规范写入流(加密后内容)

func (Postgres_t) SaveConnectorFile added in v1.0.9

func (t Postgres_t) SaveConnectorFile(onlyMainFilename string) error

提供文件名(不含后缀,模块自动添加),保存生成连接器文件

type RedisConnector added in v1.0.9

type RedisConnector string

func (RedisConnector) Connect added in v1.0.9

func (r RedisConnector) Connect() (*redis.Conn, error)

从连连接池获取一个连接. 获得一个有效连接后,可以使用封装的常用命令,也可以直接使用 conn.Do原生函数执行任何命令

func (RedisConnector) Info added in v1.0.9

func (ctr RedisConnector) Info() *Redis_t

返回连接信息,如果没有找到则连接器返回nil

type Redis_t

type Redis_t struct {
	Key           string `toml:"key" json:"key"`
	Server        string `toml:"server"`
	Port          int    `toml:"port"`
	Pwd           string `toml:"pwd"`
	DB            int    `toml:"db"`
	PoolMaxActive int    `toml:"poolMaxActive"` // 0无限制
}

func (Redis_t) CreateConnector

func (t Redis_t) CreateConnector(output io.Writer) error

将连接信息按照规范写入流(加密后内容)

func (Redis_t) SaveConnectorFile

func (t Redis_t) SaveConnectorFile(onlyMainFilename string) error

提供文件名(不含后缀,模块自动添加),保存生成连接器文件

Jump to

Keyboard shortcuts

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