db

package module
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 17 Imported by: 7

README

Go语言的一个数据库操作封装

核心思想是传入一个结果容器,根据容器的类型自动填充数据,方便使用

配置

{
  "test": {
    "type": "mysql",
    "user": "test",
    "password": "34RVCy0rQBSQmLX64xjoyg==",	// 使用 github.com/ssgo/base/passwordMaker 生成
    "host": "/tmp/mysql.sock",
    "db": "test",
    "maxOpens": 100,	// 最大连接数,0表示不限制
    "maxIdles": 30,		// 最大空闲连接,0表示不限制
    "maxLiftTime": 0	// 每个连接的存活时间,0表示永远
  }
}

API

import github.com/ssgo/db

// 自定义加密密钥,
func SetEncryptKeys(key, iv []byte){}

// 获得一个数据库操作实例,这是一个连接池,直接操作即可不需要实例化
func GetDB(name string) (*DB, error){}


// 释放数据库操作实例,正常情况下不应该操作,否则整个连接池都将无法使用
func (this *DB) Destroy() error{}

// 取得原始的 sql.DB 对象,可自行操作
func (this *DB) GetConnection() *sql.DB{}

// 查询数据,根据接收结果的对象不同执行不同的查询
// results := make([]userInfo, 0)               取全部结果,存为对象
// results := make([]map[string]interface{}, 0) 取全部结果,保留原始类型
// results := make([]map[string]string, 0)      取全部结果,统一转换为string
// results := make([]map[string]int, 0)         取全部结果,统一转换为int,不可转换会报错
// results := make([][]string, 0)               取全部结果,存为数组
// results := userInfo{}                        取第一行数据,存为对象
// results := map[string]interface{}{}          取第一行数据,存为map
// results := make([]string, 0)                 取全部第一列数据
// var results int                              取第一行第一列数据
func (this *DB) Query(results interface{}, requestSql string, args ...interface{}) error {}

// 执行普通查询,返回影响列数
func (this *DB) Exec(requestSql string, args ...interface{}) (int64, error) {}

// 执行普通INSERT或REPLACE,返回lastInsertId
func (this *DB) ExecInsert(requestSql string, args ...interface{}) (int64, error) {}

// 按数据对象自动生成INSERT语句并执行,data支持Map和Struct
func (this *DB) Insert(table string, data interface{}) (int64, error) {}

// 按数据对象自动生成REPLACE语句并执行,data支持Map和Struct
func (this *DB) Replace(table string, data interface{}) (int64, error) {}

// 按数据对象自动生成UPDATE语句并执行,data支持Map和Struct
func (this *DB) Update(table string, data interface{}, wheres string, args ...interface{}) (int64, error) {}

// 开启一个事务
func (this *DB) Begin() (*Tx, error) {}

// 预处理
func (this *DB) Prepare(requestSql string) (*Stmt, error) {}


// 在事务中操作,同(this *DB)
func (this *Tx) Query(results interface{}, requestSql string, args ...interface{}) error {}
func (this *Tx) Exec(requestSql string, args ...interface{}) (int64, error) {}
func (this *Tx) ExecInsert(requestSql string, args ...interface{}) (int64, error) {}
func (this *Tx) Insert(table string, data interface{}) (int64, error) {}
func (this *Tx) Replace(table string, data interface{}) (int64, error) {}
func (this *Tx) Update(table string, data interface{}, wheres string, args ...interface{}) (int64, error) {}
func (this *Tx) Prepare(requestSql string) (*Stmt, error) {

// 提交
func (this *Tx) Commit() error {

// 回滚
func (this *Tx) Rollback() error {


// 批量执行,返回受影响的列数
func (this *Stmt) Exec(args ...interface{}) (int64, error) {}

// 批量执行,返回lastInsertId
func (this *Stmt) ExecInsert(args ...interface{}) (int64, error) {}

// 关闭预处理
func (this *Stmt) Close() error {}

运行测试用例

把 tests/db.json.sample 复制为 tests/db.json

修改你本地数据库连接信息

使用 github.com/ssgo/base 中的 passwordMaker 创建加密后的密码写入 db.json 中的 password

进入 tests 目录运行 go test

项目中的配置

把 /db.json.sample 复制到你项目中命名为 /db.json

修改你本地数据库连接信息

使用 passwordMaker 创建加密后的密码写入 db.json 中的 password

自定义加密(强烈推荐)
把 /dbInit.go.sample 复制到你项目中命名为 /dbInit.go

修改其中的 key 和 iv 的内容,长度至少32字节,保持与passwordMaker 中一致

也可以以其他方式只要在 init 函数中调用 db.SetEncryptKeys 设置匹配的 key和iv即可

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InKeys added in v0.2.23

func InKeys(numArgs int) string

func RegisterSSL added in v0.5.49

func RegisterSSL(name, ca, cert, key string, insecure bool)

func SetEncryptKeys

func SetEncryptKeys(key, iv []byte)

Types

type DB

type DB struct {
	Config *dbInfo

	Error error
	// contains filtered or unexported fields
}

func GetDB

func GetDB(name string, logger *log.Logger) *DB

func (*DB) Begin

func (db *DB) Begin() *Tx

func (*DB) CopyByLogger added in v0.1.17

func (db *DB) CopyByLogger(logger *log.Logger) *DB

func (*DB) Delete added in v0.6.0

func (db *DB) Delete(table string, wheres string, args ...interface{}) *ExecResult

func (*DB) Destroy

func (db *DB) Destroy() error

func (*DB) Exec

func (db *DB) Exec(requestSql string, args ...interface{}) *ExecResult

func (*DB) GetLogger added in v0.1.3

func (db *DB) GetLogger() *log.Logger

func (*DB) GetOriginDB

func (db *DB) GetOriginDB() *sql.DB

func (*DB) InKeys added in v0.1.17

func (db *DB) InKeys(numArgs int) string

func (*DB) Insert

func (db *DB) Insert(table string, data interface{}) *ExecResult

func (*DB) Prepare

func (db *DB) Prepare(requestSql string) *Stmt

func (*DB) Query

func (db *DB) Query(requestSql string, args ...interface{}) *QueryResult

func (*DB) Replace

func (db *DB) Replace(table string, data interface{}) *ExecResult

func (*DB) SetLogger added in v0.1.3

func (db *DB) SetLogger(logger *log.Logger)

func (*DB) Update

func (db *DB) Update(table string, data interface{}, wheres string, args ...interface{}) *ExecResult

type ExecResult

type ExecResult struct {
	Sql   *string
	Args  []interface{}
	Error error
	// contains filtered or unexported fields
}

func (*ExecResult) Changes

func (r *ExecResult) Changes() int64

func (*ExecResult) Id

func (r *ExecResult) Id() int64

type QueryResult

type QueryResult struct {
	Sql   *string
	Args  []interface{}
	Error error
	// contains filtered or unexported fields
}

func (*QueryResult) Complete added in v0.5.43

func (r *QueryResult) Complete()

func (*QueryResult) FloatOnR1C1 added in v0.5.33

func (r *QueryResult) FloatOnR1C1() float64

func (*QueryResult) IntOnR1C1

func (r *QueryResult) IntOnR1C1() int64

func (*QueryResult) IntsOnC1

func (r *QueryResult) IntsOnC1() []int64

func (*QueryResult) MapOnR1

func (r *QueryResult) MapOnR1() map[string]interface{}

func (*QueryResult) MapResults

func (r *QueryResult) MapResults() []map[string]interface{}

func (*QueryResult) SliceResults

func (r *QueryResult) SliceResults() [][]interface{}

func (*QueryResult) StringMapOnR1 added in v0.5.49

func (r *QueryResult) StringMapOnR1() map[string]string

func (*QueryResult) StringMapResults

func (r *QueryResult) StringMapResults() []map[string]string

func (*QueryResult) StringOnR1C1

func (r *QueryResult) StringOnR1C1() string

func (*QueryResult) StringSliceResults

func (r *QueryResult) StringSliceResults() [][]string

func (*QueryResult) StringsOnC1

func (r *QueryResult) StringsOnC1() []string

func (*QueryResult) To

func (r *QueryResult) To(result interface{}) error

func (*QueryResult) ToKV added in v0.1.7

func (r *QueryResult) ToKV(target interface{}) error

type Stmt

type Stmt struct {
	Error error
	// contains filtered or unexported fields
}

func (*Stmt) Close

func (stmt *Stmt) Close() error

func (*Stmt) Exec

func (stmt *Stmt) Exec(args ...interface{}) *ExecResult

type Tx

type Tx struct {
	Error error
	// contains filtered or unexported fields
}

func (*Tx) CheckFinished added in v0.5.51

func (tx *Tx) CheckFinished() error

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) Delete added in v0.6.0

func (tx *Tx) Delete(table string, wheres string, args ...interface{}) *ExecResult

func (*Tx) Exec

func (tx *Tx) Exec(requestSql string, args ...interface{}) *ExecResult

func (*Tx) Finish added in v0.1.17

func (tx *Tx) Finish(ok bool) error

func (*Tx) Insert

func (tx *Tx) Insert(table string, data interface{}) *ExecResult

func (*Tx) Prepare

func (tx *Tx) Prepare(requestSql string) *Stmt

func (*Tx) Query

func (tx *Tx) Query(requestSql string, args ...interface{}) *QueryResult

func (*Tx) Replace

func (tx *Tx) Replace(table string, data interface{}) *ExecResult

func (*Tx) Rollback

func (tx *Tx) Rollback() error

func (*Tx) Update

func (tx *Tx) Update(table string, data interface{}, wheres string, args ...interface{}) *ExecResult

Jump to

Keyboard shortcuts

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