gxdb

package
v0.0.0-...-e027c79 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//TimeZone ...
	TimeZone = "Asia/Shanghai"
	//Custom ...
	Custom = "2006-01-02 15:04:05"
)
View Source
const (
	RedisDBCache = 1
)

cache redis constant

Variables

View Source
var DefOption = &Option{
	Username:               "root",
	Password:               "password",
	DBName:                 "g_database",
	Link:                   "tcp(127.0.0.1:3306)/{db}?charset=utf8mb4&collation=utf8mb4_bin&timeout=90s&loc=Local",
	UseCache:               false,
	ShowSQL:                true,
	MaxIdleConnections:     10,
	MaxOpenConnections:     100,
	MaxConnLifetimeSeconds: 31,
}

DefOption ...默认数据库连接参数

View Source
var DefRedisOption = &RedisOption{
	Addr:               "127.0.0.1:6379",
	Password:           "",
	DB:                 0,
	MaxRetries:         5,
	MinIdleConnections: 10,
	MaxConnAgeSeconds:  10 * 60,
}

DefRedisOption redis 连接默认参数

Functions

func SetTimeZone

func SetTimeZone() error

SetTimeZone ...Shanghai

Types

type CallBackFunc

type CallBackFunc func(InterfaceSession) error

CallBackFunc 回调函数

type CompoundIndex

type CompoundIndex struct {

	//索引字段映射关系,k:数据库中的字段名,v:struct的field
	Columns Map
	Unique  bool
	// contains filtered or unexported fields
}

CompoundIndex 复合索引

type DBInterface

type DBInterface interface {
	SetOpt(opt *Option) DBInterface
	SetOutputFilePath(fName string) DBInterface
	SetRedisClient(redisClient IRedisMaster) DBInterface
	Dial() (err error)
	RegisterTable(tables ...interface{})
	Sync() error
	Engine() xorm.EngineInterface
	Session() *Session
	CreateDB() (err error)
	RedisClient() IRedisMaster
	MemCache() *cache.Cache
}

DBInterface 数据库操作接口

var XDB DBInterface = newXdb()

XDB 全局db对象

type DbActionFunc

type DbActionFunc func() (int64, error)

DbActionFunc ...db操作回调

type IRedisMaster

type IRedisMaster interface {
	SetCfgOpt(*RedisOption) IRedisMaster
	Dial() error
	SelectDBPipe(int, func(redis.Pipeliner) error) error
	SelectDBPipeTX(int, func(redis.Pipeliner) error) error
	C() *redis.Client
	Client() *redis.Client
}

IRedisMaster ...Redis操作接口

var GRedis IRedisMaster = new(redisA)

GRedis 全局变量

type InterfaceCompoundIndexes

type InterfaceCompoundIndexes interface {
	CompoundIndexes() []*CompoundIndex
	UseCache() bool
}

InterfaceCompoundIndexes ...联合索引接口 实现了此接口的数据,将会经过Redis缓存

type InterfaceSession

type InterfaceSession interface {
	Session() *xorm.Session
	Sync(beans ...interface{}) error
	Insert(beans ...interface{}) (int64, error)
	InsertOne(bean interface{}) (int64, error)
	Update(up *Updater) (int64, error)
	Delete(conditionBean interface{}) (int64, error)
	BeginTx() (InterfaceSession, error)
	IsTx() bool
	RollBack() error
	Commit() error
	Close()
	Get(bean interface{}) (has bool, err error)
	WrapTx(callBackFunc CallBackFunc) (err error)
	PubForCache(bean interface{}) (err error)
	WrapTxForCache(bean interface{}, fn CallBackFunc) error
	NewUpdater(in interface{}) *SNUpdate
}

InterfaceSession ...数据库会话接口

type InterfaceTableInit

type InterfaceTableInit interface {
	InitData() (err error)
}

InterfaceTableInit ...数据表初始化接口

type JSONTime

type JSONTime time.Time

JSONTime ...

func Must2JSONTimeAddr

func Must2JSONTimeAddr(in string) *JSONTime

Must2JSONTimeAddr maybe panic

func NewJSONTimeOfTime

func NewJSONTimeOfTime(t time.Time) *JSONTime

NewJSONTimeOfTime 时间转换为JSONTime

func Now

func Now() *JSONTime

Now 当前时间

func ToDatetime

func ToDatetime(in string) (JSONTime, error)

ToDatetime ...

func Today

func Today() *JSONTime

Today ...今日日期

func (JSONTime) Addr

func (p JSONTime) Addr() *JSONTime

Addr ...

func (*JSONTime) Convert2Time

func (p *JSONTime) Convert2Time() time.Time

Convert2Time ...

func (*JSONTime) Date

func (p *JSONTime) Date() *JSONTime

Date ...返回一个日期0点的时间

func (*JSONTime) FromDB

func (p *JSONTime) FromDB(data []byte) error

FromDB ...

func (*JSONTime) GobDecode

func (p *JSONTime) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface.

func (*JSONTime) GobEncode

func (p *JSONTime) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (*JSONTime) MarshalJSON

func (p *JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*JSONTime) Scan

func (p *JSONTime) Scan(v interface{}) error

Scan valueof time.Time

func (*JSONTime) SetByTime

func (p *JSONTime) SetByTime(timeVal time.Time)

SetByTime ...

func (*JSONTime) String

func (p *JSONTime) String() string

String ...

func (*JSONTime) StringFormat

func (p *JSONTime) StringFormat(layout string) string

StringFormat 转换为固定格式字符串

func (*JSONTime) Time

func (p *JSONTime) Time() time.Time

Time ...

func (*JSONTime) ToDB

func (p *JSONTime) ToDB() (b []byte, err error)

ToDB ...

func (*JSONTime) UnmarshalJSON

func (p *JSONTime) UnmarshalJSON(data []byte) error

UnmarshalJSON ...

func (*JSONTime) Value

func (p *JSONTime) Value() (driver.Value, error)

Value insert timestamp into mysql need this function.

type Map

type Map map[string]interface{}

Map 映射

type MyBase

type MyBase struct {
	ID      int64     `json:"id,omitempty" xorm:"pk autoincr"`
	Version int64     `json:"version,omitempty" xorm:"notnull version"`
	Created *JSONTime `json:"created,omitempty" xorm:"notnull created index"`
	Updated *JSONTime `json:"updated,omitempty" xorm:"notnull updated"`
	// contains filtered or unexported fields
}

MyBase xorm MySQL model base

func (*MyBase) SetSn

func (b *MyBase) SetSn(sn InterfaceSession)

SetSn ...设定上下文sql session,保证事务

func (*MyBase) Sn

func (b *MyBase) Sn() InterfaceSession

Sn ... session 对象,如果上下文session对象为空,则初始化Engine session

type Option

type Option struct {
	Username               string `yaml:"username" mapstructure:"username"`
	Password               string `yaml:"password" mapstructure:"password"`
	DBName                 string `yaml:"db_name" mapstructure:"db_name"`
	Link                   string `yaml:"link" mapstructure:"link"`
	UseCache               bool   `yaml:"use_cache" mapstructure:"use_cache"`
	ShowSQL                bool   `yaml:"show_sql" mapstructure:"show_sql"`
	MaxIdleConnections     int    `yaml:"max_idle_connections" mapstructure:"max_idle_connections"`
	MaxOpenConnections     int    `yaml:"max_open_connections" mapstructure:"max_open_connections"`
	MaxConnLifetimeSeconds int    `yaml:"max_conn_lifetime_seconds" mapstructure:"max_conn_lifetime_seconds"`
}

Option ...配置信息

type RedisOption

type RedisOption struct {
	// The network type, either tcp or unix.
	// Default is tcp.
	//Network string
	// host:port address.
	Addr string `yaml:"addr" mapstructure:"addr"`

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string `yaml:"password" mapstructure:"password"`
	// Database to be selected after connecting to the server.
	DB int `yaml:"db" mapstructure:"db"`

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int `yaml:"max_retries" mapstructure:"max_retries"`

	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	//PoolSize int
	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	//空闲连接数
	MinIdleConnections int `yaml:"min_idle_connections" mapstructure:"min_idle_connections"`
	// Connection age at which client retires (closes) the connection.
	// Default is to not close aged connections.
	//连接状态最长时间,超出时间,会被强制关闭,建议10分钟
	MaxConnAgeSeconds int `yaml:"max_conn_age_seconds" mapstructure:"max_conn_age_seconds"`
}

RedisOption 配置参数

type SNUpdate

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

SNUpdate ...

func (*SNUpdate) Err

func (s *SNUpdate) Err() error

Err ...获取当前操作的错误

func (*SNUpdate) Exec

func (s *SNUpdate) Exec(fn func(out interface{}, sn InterfaceSession) error) *SNUpdate

Exec ...执行数据库操作 判断当前任务是否在一个事务中,自动将任务放入事务

func (*SNUpdate) Next

func (s *SNUpdate) Next(fn func(out interface{}) error) *SNUpdate

Next ...

func (*SNUpdate) Out

func (s *SNUpdate) Out() interface{}

Out ...获取当前更新的结果对象

type Session

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

Session 数据会话

func (*Session) BeginTx

func (s *Session) BeginTx() (newSn InterfaceSession, err error)

BeginTx 开启一个事务

func (*Session) Close

func (s *Session) Close()

Close ...关闭会话

func (*Session) Commit

func (s *Session) Commit() error

Commit ...事务提交

func (*Session) Delete

func (s *Session) Delete(conditionBean interface{}) (int64, error)

Delete 删除一条数据 先根据结构体条件查询,查询出错,则不对数据库操作

func (*Session) Get

func (s *Session) Get(bean interface{}) (has bool, err error)

Get ...经过内存缓存,Redis缓存的get操作

func (*Session) Insert

func (s *Session) Insert(beans ...interface{}) (int64, error)

Insert ...需要触发缓存操作,必须逐条插入; 传入的参数,必须都是切片 如果确定对象不使用缓存,则可以使用orm Session Insert 进行批量插入

func (*Session) InsertOne

func (s *Session) InsertOne(bean interface{}) (int64, error)

InsertOne ...单条插入数据

func (*Session) IsTx

func (s *Session) IsTx() bool

IsTx 是否是一个事务会话

func (*Session) NewUpdater

func (s *Session) NewUpdater(condition interface{}) (up *SNUpdate)

NewUpdater ...链式操作的update

func (*Session) PubForCache

func (s *Session) PubForCache(bean interface{}) (err error)

PubForCache ... 发布删除缓存主题,并删除Redis缓存

func (*Session) RollBack

func (s *Session) RollBack() error

RollBack ...事务回滚

func (*Session) Session

func (s *Session) Session() *xorm.Session

Session ...获取orm Session

func (*Session) Sync

func (s *Session) Sync(beans ...interface{}) error

Sync ...初始化表数据 扫描对象,观察是否需要初始化数据 是否需要创建联合索引

func (*Session) Update

func (s *Session) Update(up *Updater) (int64, error)

Update 更新操作,回调函数的参数是从缓存中查出来的值,可以用于替换更新内容

func (*Session) WrapTx

func (s *Session) WrapTx(callBackFunc CallBackFunc) (err error)

WrapTx 开启一个事务处理函数

func (*Session) WrapTxForCache

func (s *Session) WrapTxForCache(bean interface{}, fn CallBackFunc) error

WrapTxForCache ...发布删除缓存的消息 bean 带有查询条件的对象 fn 数据库操作 第一个参数,删除缓存需要的对象 第二个参数,是一个数据库执行方法

type Updater

type Updater struct {
	ReqData          interface{}
	CheckAndBindFunc func(out interface{}) error
	UpdateFunc       func(sn InterfaceSession, out interface{}) (int64, error)
}

Updater ...

Jump to

Keyboard shortcuts

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