ckdb

package
v1.6.7 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

数据库处理

Index

Constants

View Source
const (
	DB_DRIVER_MYSQL  = "mysql"
	DB_DRIVER_SQLITE = "sqlite"
)

Variables

View Source
var MysqlDrivers = make(map[string]*sql.DB)
View Source
var SqliteDrivers = make(map[string]*sql.DB)

Functions

func BuildTableStruct

func BuildTableStruct(tableName, dbName string, dbConf *DBConfig)

输出表结构为GO struct

func BuildTableStructSqlType added in v1.2.9

func BuildTableStructSqlType(tableName, dbName string, dbConf *DBConfig, isSqlType bool)

func InitDB

func InitDB(db_dsn string, pool_size int) error

old init

func InitDb added in v1.0.2

func InitDb(conf *DBConfig) (*sql.DB, error)

func InitMongo

func InitMongo(conf *MongoDBConfig) error

new init mongodb

func InitMysqlDb

func InitMysqlDb(conf *DBConfig) (*sql.DB, error)

func InitSqliteDb added in v1.0.2

func InitSqliteDb(conf *DBConfig) (*sql.DB, error)

Types

type BoltDB

type BoltDB struct {
	// contains filtered or unexported fields
}

bolt DB 数据库处理

func NewBoltDB

func NewBoltDB(filepath string) *BoltDB

创建一个新的 BlotDB

func (*BoltDB) Close

func (b *BoltDB) Close()

关闭数据库

func (*BoltDB) Delete

func (b *BoltDB) Delete(bucket_name string, key ...string) error

删除一个值

func (*BoltDB) ForEach

func (b *BoltDB) ForEach(bucketName string, callback func([]byte, []byte) error) error

迭代一个bucket里所有的数据

func (*BoltDB) Get

func (b *BoltDB) Get(bucket_name string, key string) ([]byte, error)

从 bucket 传入 KEY 得到一个值

func (*BoltDB) Path

func (b *BoltDB) Path() string

返回当前 bolt DB 位置

func (*BoltDB) Put

func (b *BoltDB) Put(bucket_name string, key string, val interface{}) error

插一个新的 key value 值到 bucket

type CKCollection

type CKCollection struct {
	// contains filtered or unexported fields
}

func NewCollection

func NewCollection(mdb *DBMongo, tab_name string) *CKCollection

func (*CKCollection) Aggregate

func (ck *CKCollection) Aggregate(pipe ...bson.M) (bson.M, error)

Aggregate 执行聚合操作

func (*CKCollection) AggregateAllowDiskUse added in v1.5.3

func (ck *CKCollection) AggregateAllowDiskUse(pipe ...bson.M) (bson.M, error)

AggregateAllowDiskUse 执行聚合操作 allowDiskUse

func (*CKCollection) Close

func (ck *CKCollection) Close()

Close 关闭数据库连接

func (*CKCollection) Count

func (ck *CKCollection) Count(where bson.M) int

Count 得到所给条件的数据量

func (*CKCollection) Delete

func (ck *CKCollection) Delete(where bson.M) error

Delete 删除数据

func (*CKCollection) Drop

func (ck *CKCollection) Drop() error

Drop 删除 Collection

func (*CKCollection) Find

func (ck *CKCollection) Find(where bson.M, row interface{}) error

查找数据

func (*CKCollection) Insert

func (ck *CKCollection) Insert(rows ...interface{}) error

Insert 插入数据

func (*CKCollection) List

func (ck *CKCollection) List(where bson.M, page int, number int, sort_list []string, struct_type interface{}, format func(interface{})) ([]interface{}, error)

List 查询数据库返回列表

func (*CKCollection) Query

func (ck *CKCollection) Query(where bson.M, page int, number int, sort_list []string, struct_type interface{}, format func(interface{})) (*QueryResult, error)

Query 查询数据库

func (*CKCollection) Update

func (ck *CKCollection) Update(where bson.M, update interface{}) error

Update 更新数据

func (*CKCollection) UpdateAll

func (ck *CKCollection) UpdateAll(where bson.M, update interface{}) (*mgo.ChangeInfo, error)

UpdateAll 更新所有条件数据

func (*CKCollection) Upset

func (ck *CKCollection) Upset(where bson.M, update interface{}) error

type DBA

type DBA struct {
	LastSql   string
	LastArgs  []interface{}
	LastError error
	// contains filtered or unexported fields
}

func NewDBA

func NewDBA(dbConf *DBConfig) (*DBA, error)

新创建DBA操作库

func (*DBA) BeginTrans

func (d *DBA) BeginTrans() error

开始事务

func (*DBA) Close

func (d *DBA) Close()

func (*DBA) Commit

func (d *DBA) Commit() error

提交事务

func (*DBA) ConvertData

func (d *DBA) ConvertData(orgData interface{}) (utils.M, error)

func (*DBA) Delete

func (d *DBA) Delete(where utils.M, table string) (int, error)

条件删除数据

func (*DBA) Exec

func (d *DBA) Exec(sqlStr string, args ...interface{}) (sql.Result, error)

执行SQL语句

func (*DBA) FetchAll

func (d *DBA) FetchAll(query *sql.Rows) ([]utils.M, error)

取得所有数据

func (*DBA) FetchAllOfStruct

func (d *DBA) FetchAllOfStruct(query *sql.Rows, i interface{}) ([]interface{}, error)

取得所有数据到结构体,没传结构体为默认 utils.M

func (*DBA) FetchAllOfStructV2

func (d *DBA) FetchAllOfStructV2(query *sql.Rows, i interface{}) ([]interface{}, error)

取得所有数据到结构体,没传结构体为默认 utils.M

func (*DBA) FormatColumn

func (d *DBA) FormatColumn(column string) string

格式化字段名

func (*DBA) GetLastError

func (d *DBA) GetLastError() error

得到最后一条错误

func (*DBA) HaltError

func (d *DBA) HaltError(err error)

错误处理

func (*DBA) Insert

func (d *DBA) Insert(table string, orgData interface{}) (int, bool)

插入记录到数据库

func (*DBA) InsertMulti added in v1.0.1

func (d *DBA) InsertMulti(table string, dataList []interface{}) (int, bool)

插入多条记录

func (*DBA) Query

func (d *DBA) Query(sqlStr string, args ...interface{}) ([]utils.M, error)

查询数据库

func (*DBA) QueryOne

func (d *DBA) QueryOne(sqlStr string, args ...interface{}) (utils.M, error)

查询一条记录

func (*DBA) QueryOneStruct

func (d *DBA) QueryOneStruct(structInterFace interface{}, sqlStr string, args ...interface{}) (interface{}, error)

查询一条记录返回结构体

func (*DBA) QueryRow

func (d *DBA) QueryRow(sqlStr string, args ...interface{}) *sql.Row

func (*DBA) QueryStruct

func (d *DBA) QueryStruct(structInterFace interface{}, sqlStr string, args ...interface{}) ([]interface{}, error)

查询数据库并返回结果 (传入结构体)

func (*DBA) Rollback

func (d *DBA) Rollback() error

回滚事务

func (*DBA) SetQueryInterface

func (d *DBA) SetQueryInterface(i interface{})

func (*DBA) Stats added in v1.2.3

func (d *DBA) Stats() sql.DBStats

return mysql status

func (*DBA) Table

func (d *DBA) Table(tableName string) *DBATable

设置操作的表名

func (*DBA) Update

func (d *DBA) Update(data utils.M, where utils.M, table string) error

更新数据库数据

func (*DBA) UpdateAny added in v1.0.7

func (d *DBA) UpdateAny(data interface{}, where utils.M, table string) error

更新整条数据

func (*DBA) WhereRecursion

func (d *DBA) WhereRecursion(where utils.M, icon string, table string) (string, []interface{})

处理where条件列表

type DBATable

type DBATable struct {
	// contains filtered or unexported fields
}

Table 主结构

func NewDBATable

func NewDBATable(db *DBA, table string) *DBATable

新建一个table处理类

func (*DBATable) BeginTrans

func (t *DBATable) BeginTrans() error

开始事务

func (*DBATable) Clear

func (t *DBATable) Clear()

清除所有查询条件

func (*DBATable) Commit

func (t *DBATable) Commit() error

提交事务

func (*DBATable) Delete

func (t *DBATable) Delete() bool

删除数据

func (*DBATable) Find

func (t *DBATable) Find() (utils.M, error)

只得到一条记录

func (*DBATable) FindStruct

func (t *DBATable) FindStruct(i interface{}) (interface{}, error)

只得到一条记录,以传入类型返回该类型数组

func (*DBATable) Insert

func (t *DBATable) Insert(data interface{}) (int, bool)

插入数据

func (*DBATable) InsertMulti added in v1.0.1

func (t *DBATable) InsertMulti(dataList []interface{}) (int, bool)

插入多条数据

func (*DBATable) Join

func (t *DBATable) Join(fields [][]string) *DBATable

添加多外JOIN

func (*DBATable) JoinOne

func (t *DBATable) JoinOne(join *TBJoin) *DBATable

添加一条JOIN记录

func (*DBATable) Limit

func (t *DBATable) Limit(number int, page int) *DBATable

设置分页

func (*DBATable) One

func (t *DBATable) One(rowStruct interface{}) error

func (*DBATable) Order

func (t *DBATable) Order(orders utils.M) *DBATable

设置ORDER 排序

func (*DBATable) OrderIn

func (t *DBATable) OrderIn(column string, rule []string) *DBATable

ORDER IN 条件排序

func (*DBATable) Query

func (t *DBATable) Query() *DBATable

开始查询

func (*DBATable) Result

func (t *DBATable) Result() ([]utils.M, error)

得到所有列表结果集

func (*DBATable) ResultStruct

func (t *DBATable) ResultStruct(i interface{}) ([]interface{}, error)

得到所有列表结果集,以传入类型返回该类型数组

func (*DBATable) Rollback

func (t *DBATable) Rollback() error

回滚事务

func (*DBATable) Rows

func (t *DBATable) Rows() int

得到记录条数

func (*DBATable) Select

func (t *DBATable) Select(fields utils.M) *DBATable

设置要显示的字段

func (*DBATable) Update

func (t *DBATable) Update(data utils.M) bool

更新数据

func (*DBATable) UpdateAny added in v1.0.7

func (t *DBATable) UpdateAny(data interface{}) bool

更新整条记录

func (*DBATable) Where

func (t *DBATable) Where(fields utils.M, table string) *DBATable

设置WHERE条件

func (*DBATable) WhereAnd added in v1.4.9

func (t *DBATable) WhereAnd(fields utils.M, table string) *DBATable

设置where and

func (*DBATable) WhereOr added in v1.4.9

func (t *DBATable) WhereOr(fields utils.M, table string) *DBATable

设置where and

type DBColumn

type DBColumn struct {
	Field string
	Icon  string
}

type DBConfig

type DBConfig struct {
	DBDriver   string `json:"db_driver" yaml:"db_driver"`
	DBServer   string `json:"db_server" yaml:"db_server"`
	DBPort     string `json:"db_port" yaml:"db_port"`
	DBName     string `json:"db_name" yaml:"db_name"`
	DBUser     string `json:"db_user" yaml:"db_user"`
	DBPassword string `json:"db_password" yaml:"db_password"`
	DBPoolSize int    `json:"db_pool_size" yaml:"db_pool_size"`
	DBIdleSize int    `json:"db_Idle_size" yaml:"db_Idle_size"`
	MaxLife    int    `json:"max_life" yaml:"max_life"`
	DBDebug    bool   `json:"db_debug" yaml:"db_debug"`
}

数据库配置

type DBMongo

type DBMongo struct {
	// contains filtered or unexported fields
}

func NewDB

func NewDB(db_name string) *DBMongo

func (*DBMongo) ChangeDB

func (this *DBMongo) ChangeDB(db_name string)

func (*DBMongo) Close

func (this *DBMongo) Close()

func (*DBMongo) Collection

func (this *DBMongo) Collection(collection_name string) *CKCollection

func (*DBMongo) Open

func (this *DBMongo) Open(db_name string) *DBMongo

func (*DBMongo) RunCmd

func (this *DBMongo) RunCmd()

func (*DBMongo) Table

func (this *DBMongo) Table(tab_name string) *mgo.Collection

type DM

type DM map[string]interface{}

DBA专用数据

type MongoDBConfig

type MongoDBConfig struct {
	DBHost     string `json:"db_host" yaml:"db_host"`
	DBPort     string `json:"db_port" yaml:"db_port"`
	DBUser     string `json:"db_user" yaml:"db_user"`
	DBPasswd   string `json:"db_passwd" yaml:"db_passwd"`
	DBAuth     string `json:"db_auth" yaml:"db_auth"`
	DBName     string `json:"db_name" yaml:"db_name"`
	DBPoolSize int    `json:"db_pool_size" yaml:"db_pool_size"`
}

dsn mongodb://root:WiaQ82n7B3L5Cz*2#10m@172.18.76.150:27017?authSource=admin

func (*MongoDBConfig) BuildDsn

func (mc *MongoDBConfig) BuildDsn() string

build dsn string

type QueryResult

type QueryResult struct {
	List  interface{} `json:"list"`
	Count int         `json:"count"`
}

查询结果

type TBField

type TBField struct {
	Column string //字段名
	Alias  string //别名
	Func   string //所用方法
}

type TBJoin

type TBJoin struct {
	TableTo    string
	ColumnTo   string
	TableFrom  string
	ColumnFrom string
	Key        string
}

Jump to

Keyboard shortcuts

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