Categorygithub.com/tinybear1976/dbconnector
modulepackage
1.1.3
Repository: https://github.com/tinybear1976/dbconnector.git
Documentation: pkg.go.dev

# 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()
    //...
}

# Functions

从指定文件中读取连接器信息,并进行解析,自动添加.
根据指定路径搜索所有连接器文件,并进行解析,自动添加.
从连接配置结构体切片中进行添加,此函数一般用于第二步,从配置文件中读取后,进行添加,如果与之前的文件连接器key冲突,这里将覆盖之前的内容.
只增加Mariadb连接.
只增加Postgres连接.
只增加Redis连接.
No description provided by the author
清除所有的连接器.
清除所有的连接器.
清除所有的连接器.
解密一个指定的连接器文件,只还原字符串原型,不做进一步解析.
No description provided by the author
断开连接.
No description provided by the author
常用命令GET.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author