Documentation ¶
Index ¶
- Constants
- Variables
- func AllDAOMapping() map[string]DAOInterface
- func IsPrepareError(err error) bool
- func NewDAO(daoPointer any) any
- func NotifyReady()
- func OnDAOInitBefore(f func(dao DAOInterface))
- func OnDAOInitError(f func(dao DAOInterface, err error) error)
- func OnReady(f func())
- func OnReadyDone(f func())
- type Config
- type DAO
- type DAOInterface
- type DAOObject
- func (this *DAOObject) Delete(tx *Tx, pk any) (rowsAffected int64, err error)
- func (this *DAOObject) Exist(tx *Tx, pk any) (bool bool, err error)
- func (this *DAOObject) Find(tx *Tx, pk any) (modelPtr any, err error)
- func (this *DAOObject) Init() error
- func (this *DAOObject) NotifyDelete() error
- func (this *DAOObject) NotifyInsert() error
- func (this *DAOObject) NotifyUpdate() error
- func (this *DAOObject) Object() *DAOObject
- func (this *DAOObject) OnDelete(callback func() error)
- func (this *DAOObject) OnInsert(callback func() error)
- func (this *DAOObject) OnUpdate(callback func() error)
- func (this *DAOObject) Query(tx *Tx) *Query
- func (this *DAOObject) Save(tx *Tx, operatorPtr any) (err error)
- func (this *DAOObject) SaveInt64(tx *Tx, operatorPtr any) (pkValue int64, err error)
- type DAOWrapper
- type DB
- func (this *DB) Begin() (*Tx, error)
- func (this *DB) Close() error
- func (this *DB) Config() (*DBConfig, error)
- func (this *DB) Driver() string
- func (db *DB) Exec(query string, params ...any) (sql.Result, error)
- func (this *DB) FindCol(colIndex int, query string, args ...any) (any, error)
- func (this *DB) FindFullTable(tableName string) (*Table, error)
- func (this *DB) FindFunctions() ([]*Function, error)
- func (this *DB) FindOne(query string, args ...any) (maps.Map, error)
- func (this *DB) FindOnes(query string, args ...any) (ones []maps.Map, columnNames []string, err error)
- func (this *DB) FindPreparedOnes(query string, args ...any) (results []maps.Map, columnNames []string, err error)
- func (this *DB) FindTable(tableName string) (*Table, error)
- func (this *DB) Id() string
- func (this *DB) Name() string
- func (this *DB) Prepare(query string) (*Stmt, error)
- func (this *DB) PrepareOnce(query string) (*Stmt, bool, error)
- func (this *DB) Raw() *sql.DB
- func (this *DB) RunTx(callback func(tx *Tx) error) error
- func (this *DB) SetConfig(config *DBConfig)
- func (this *DB) StmtManager() *StmtManager
- func (this *DB) TableNames() ([]string, error)
- func (this *DB) TablePrefix() string
- type DBConfig
- type DBFunc
- func FuncAbs(x any) *DBFunc
- func FuncAcos(x any) *DBFunc
- func FuncAscii(str any) *DBFunc
- func FuncAsin(x any) *DBFunc
- func FuncAtan(x any) *DBFunc
- func FuncAtanXY(x any, y any) *DBFunc
- func FuncBin(str any) *DBFunc
- func FuncBitLength(str any) *DBFunc
- func FuncConcat(strs ...any) *DBFunc
- func FuncConcatWs(separator any, strs ...any) *DBFunc
- func FuncFindInSet(str any, strList any) *DBFunc
- func FuncFromUnixtime(timestamp any) *DBFunc
- func FuncFromUnixtimeFormat(timestamp any, format any) *DBFunc
- func FuncLeft(str any, len any) *DBFunc
- func FuncLength(str any) *DBFunc
- func FuncLower(str any) *DBFunc
- func FuncLpad(str any, len any, padStr any) *DBFunc
- func FuncLtrim(str any) *DBFunc
- func FuncRand() *DBFunc
- func FuncRandN(seed any) *DBFunc
- func FuncRepeat(str any, count any) *DBFunc
- func FuncReplace(str any, fromStr any, toStr any) *DBFunc
- func FuncReverse(str any) *DBFunc
- func FuncRight(str any, len any) *DBFunc
- func FuncRpad(str any, len any, padStr any) *DBFunc
- func FuncRtrim(str any) *DBFunc
- func FuncSubstring(str any, pos any) *DBFunc
- func FuncSubstringIndex(str any, delim any, count any) *DBFunc
- func FuncSubstringLen(str any, pos any, len any) *DBFunc
- func FuncTrim(str any) *DBFunc
- func FuncUpper(str any) *DBFunc
- type DBFuncParams
- type Field
- type FieldName
- type Function
- type JSON
- type Model
- type Query
- func (this *Query) AsSQL() (string, error)
- func (this *Query) Asc(fields ...string) *Query
- func (this *Query) AscPk() *Query
- func (this *Query) Attr(name string, value any) *Query
- func (this *Query) Attrs(attrs maps.Map) *Query
- func (this *Query) Avg(attr string, defaultValue float64) (float64, error)
- func (this *Query) Between(field string, min any, max any) *Query
- func (this *Query) Count() (int64, error)
- func (this *Query) CountAttr(attr string) (int64, error)
- func (this *Query) CountInt() (int, error)
- func (this *Query) DAO(dao *DAOObject) *Query
- func (this *Query) DB(db *DB) *Query
- func (this *Query) Debug(debug bool) *Query
- func (this *Query) Decrease(field string, count int) *Query
- func (this *Query) Delete() (rowsAffected int64, err error)
- func (this *Query) DeleteQuickly() error
- func (this *Query) Desc(fields ...string) *Query
- func (this *Query) DescPk() *Query
- func (this *Query) Exec() (*Result, error)
- func (this *Query) Exist() (bool, error)
- func (this *Query) Filter(filterFn func(one maps.Map) bool) *Query
- func (this *Query) Find() (any, error)
- func (this *Query) FindAll() ([]any, error)
- func (this *Query) FindBoolCol() (bool, error)
- func (this *Query) FindBytesCol() ([]byte, error)
- func (this *Query) FindCol(defaultValue any) (any, error)
- func (this *Query) FindFloat32Col(defaultValue float32) (float32, error)
- func (this *Query) FindFloat64Col(defaultValue float64) (float64, error)
- func (this *Query) FindInt64Col(defaultValue int64) (int64, error)
- func (this *Query) FindIntCol(defaultValue int) (int, error)
- func (this *Query) FindJSONCol() ([]byte, error)
- func (this *Query) FindOne() (results maps.Map, columnNames []string, err error)
- func (this *Query) FindOnes() (ones []maps.Map, columnNames []string, err error)
- func (this *Query) FindStringCol(defaultValue string) (string, error)
- func (this *Query) For(clause string) *Query
- func (this *Query) ForceIndex(index ...string) *Query
- func (this *Query) Group(field string, order ...int) *Query
- func (this *Query) Gt(attr string, value any) *Query
- func (this *Query) Gte(attr string, value any) *Query
- func (this *Query) HasJoins() bool
- func (this *Query) Having(cond string) *Query
- func (this *Query) IgnoreIndex(index ...string) *Query
- func (this *Query) Increase(field string, count int) *Query
- func (this *Query) Insert() (lastInsertId int64, err error)
- func (this *Query) InsertOrUpdate(insertingValues maps.Map, updatingValues maps.Map) (rowsAffected int64, lastInsertId int64, err error)
- func (this *Query) InsertOrUpdateQuickly(insertingValues maps.Map, updatingValues maps.Map) error
- func (this *Query) JSONContains(attr string, value any) *Query
- func (this *Query) Join(dao any, joinType int, on string) *Query
- func (this *Query) Like(field string, expr string) *Query
- func (this *Query) Limit(size int64) *Query
- func (this *Query) Lock(lock string) *Query
- func (this *Query) Lt(attr string, value any) *Query
- func (this *Query) Lte(attr string, value any) *Query
- func (this *Query) Map(mapFn func(one maps.Map) maps.Map) *Query
- func (this *Query) Max(attr string, defaultValue float64) (float64, error)
- func (this *Query) MaxInt(attr string, defaultValue int) (int, error)
- func (this *Query) MaxInt64(attr string, defaultValue int64) (int64, error)
- func (this *Query) Min(attr string, defaultValue float64) (float64, error)
- func (this *Query) Neq(attr string, value any) *Query
- func (this *Query) NoPk(noPk bool) *Query
- func (this *Query) Offset(offset int64) *Query
- func (this *Query) Order(field any, orderType int) *Query
- func (this *Query) Param(name string, value any) *Query
- func (this *Query) Partitions(partitions ...string) *Query
- func (this *Query) Pk(pks ...any) *Query
- func (this *Query) PkName(pkName string) *Query
- func (this *Query) Replace() (rowsAffected int64, lastInsertId int64, err error)
- func (this *Query) Result(fields ...any) *Query
- func (this *Query) ResultExcept(fields ...string) *Query
- func (this *Query) ResultPk() *Query
- func (this *Query) Reuse(canReuse bool) *Query
- func (this *Query) SQL(sql string) *Query
- func (this *Query) SQLCache(sqlCache int) *Query
- func (this *Query) Set(field string, value any) *Query
- func (this *Query) Sets(values map[string]any) *Query
- func (this *Query) Size(size int64) *Query
- func (this *Query) Slice(slicePtr any) *Query
- func (this *Query) State(state any) *Query
- func (this *Query) Sum(attr string, defaultValue float64) (float64, error)
- func (this *Query) SumInt(attr string, defaultValue int) (int, error)
- func (this *Query) SumInt64(attr string, defaultValue int64) (int64, error)
- func (this *Query) Table(table string) *Query
- func (this *Query) Tx(tx *Tx) *Query
- func (this *Query) Update() (rowsAffected int64, err error)
- func (this *Query) UpdateQuickly() error
- func (this *Query) UseIndex(index ...string) *Query
- func (this *Query) Where(wheres ...string) *Query
- type QueryGroup
- type QueryJoin
- type QueryOrder
- type QueryUseIndex
- type Result
- type Rows
- type SQL
- type SQLExecutor
- type Stmt
- func (this *Stmt) AccessAt() int64
- func (this *Stmt) Close() error
- func (this *Stmt) Exec(args ...any) (sql.Result, error)
- func (this *Stmt) FindCol(colIndex int, args ...any) (colValue any, err error)
- func (this *Stmt) FindOne(args ...any) (one maps.Map, err error)
- func (this *Stmt) FindOnes(args ...any) (ones []maps.Map, columnNames []string, err error)
- func (this *Stmt) Query(args ...any) (*sql.Rows, error)
- func (this *Stmt) Raw() *sql.Stmt
- type StmtManager
- func (this *StmtManager) Close() error
- func (this *StmtManager) CloseId(parentId int64) error
- func (this *StmtManager) Len() int
- func (this *StmtManager) Prepare(preparer sqlPreparer, querySQL string) (*Stmt, error)
- func (this *StmtManager) PrepareOnce(preparer sqlPreparer, querySQL string, parentId int64) (resultStmt *Stmt, wasCached bool, err error)
- func (this *StmtManager) SetMaxCount(count int)
- type Table
- type TableIndex
- type TablePartition
- type Tx
- func (this *Tx) Commit() error
- func (this *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (this *Tx) FindCol(colIndex int, query string, args ...any) (colValue any, err error)
- func (this *Tx) FindOne(query string, args ...any) (one maps.Map, err error)
- func (this *Tx) FindOnes(query string, args ...any) (ones []maps.Map, columnNames []string, err error)
- func (this *Tx) Prepare(query string) (*Stmt, error)
- func (this *Tx) PrepareOnce(query string) (*Stmt, bool, error)
- func (this *Tx) Query(query string, args ...any) (*sql.Rows, error)
- func (this *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (this *Tx) QueryRow(query string, args ...any) *sql.Row
- func (this *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (this *Tx) Raw() *sql.Tx
- func (this *Tx) Rollback() error
Constants ¶
const ( ValueTypeInvalid byte = iota ValueTypeBool ValueTypeNumber ValueTypeString ValueTypeBytes ValueTypeJSON ValueTypeTime // @TODO 暂未实现 )
const ( QueryActionFind = 1 QueryActionDelete = 2 QueryActionInsert = 3 QueryActionReplace = 4 QueryActionInsertOrUpdate = 5 QueryActionUpdate = 6 QueryActionExec = 7 )
const ( QuerySubActionCount = 1 QuerySubActionSum = 2 QuerySubActionMax = 3 QuerySubActionMin = 4 QuerySubActionAvg = 5 )
const ( QueryOrderDefault = 0 QueryOrderAsc = 1 QueryOrderDesc = 2 )
const ( QueryJoinDefault = 0 QueryJoinLeft = 1 QueryJoinRight = 2 )
const ( QueryLockForUpdate = "FOR UPDATE" )
const ( QuerySqlCacheDefault = 0 QuerySqlCacheOn = 1 QuerySqlCacheOff = 2 )
const ( QueryForAll = "" QueryForJoin = "JOIN" QueryForOrderBy = "ORDER BY" QueryForGroupBy = "GROUP BY" )
Variables ¶
var ErrNotFound = errors.New("record not found")
ErrNotFound 错误信息
var ErrTableNotFound = errors.New("table not found")
var (
ShowPreparedStatements = false
)
Functions ¶
func AllDAOMapping ¶
func AllDAOMapping() map[string]DAOInterface
func IsPrepareError ¶
func OnDAOInitBefore ¶
func OnDAOInitBefore(f func(dao DAOInterface))
func OnDAOInitError ¶
func OnDAOInitError(f func(dao DAOInterface, err error) error)
Types ¶
type Config ¶
type Config struct { Default struct { DB string `yaml:"db"` Prefix string `yaml:"prefix"` } `yaml:"default"` DBs map[string]*DBConfig `yaml:"dbs"` Fields map[string][]string `yaml:"fields"` }
func GlobalConfig ¶
func GlobalConfig() *Config
type DAOInterface ¶
type DAOInterface interface {
Object() *DAOObject
}
type DAOObject ¶
type DAOObject struct { Instance *DB DB string Table string PkName string Model any // contains filtered or unexported fields }
func (*DAOObject) NotifyDelete ¶
NotifyDelete 触发Delete回调
func (*DAOObject) NotifyInsert ¶
NotifyInsert 触发Insert回调
func (*DAOObject) NotifyUpdate ¶
NotifyUpdate 触发Update回调
type DAOWrapper ¶
type DAOWrapper interface {
Object() *DAOObject
}
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func NewInstance ¶
NewInstance 根据ID获取一个新的数据库实例 不会从上下文的缓存中读取
func NewInstanceFromConfig ¶
NewInstanceFromConfig 从配置中构造实例
func (*DB) FindFullTable ¶
FindFullTable 获取数据表,并包含分区,索引信息
func (*DB) FindFunctions ¶
FindFunctions 查找所有函数
func (*DB) FindPreparedOnes ¶
type DBConfig ¶
type DBConfig struct { Driver string `yaml:"driver"` Dsn string `yaml:"dsn"` Prefix string `yaml:"prefix,omitempty"` Connections struct { Pool int `yaml:"pool"` Max int `yaml:"max"` Life string `yaml:"life"` LifeDuration time.Duration `yaml:",omitempty"` } `yaml:"connections"` Models struct { Package string `yaml:"package"` } `yaml:"models"` }
type DBFunc ¶
func FuncAtanXY ¶
func FuncBitLength ¶
func FuncConcat ¶
func FuncConcatWs ¶
func FuncFindInSet ¶
func FuncFromUnixtime ¶
func FuncFromUnixtimeFormat ¶
func FuncLength ¶
func FuncRepeat ¶
func FuncReverse ¶
func FuncSubstring ¶
type DBFuncParams ¶
type DBFuncParams []any
type Field ¶
type Field struct { Name string // 字段名 Type string // 字段类型 FullType string // 完整的字段类型,包含长度等附加信息 DataKind reflect.Kind // 数据类型 Length int // @TODO 暂未实现 DefaultValueString string DefaultValue any AutoIncrement bool IsPrimaryKey bool // 是否为主键 IsNotNull bool // 是否为非NULL IsUnsigned bool // 是否为无符号 Comment string // 字段的注释说明 Collation string // 字符集信息 ValueType byte // 值类型,ValueType* Extra string MappingName string // 映射的名称 MappingKindName string // 映射的数据类型字符串格式 }
func (*Field) Definition ¶
func (*Field) ValueTypeName ¶
type Model ¶
type Model struct { CountFields int Attrs []string Fields []string Kinds []reflect.Kind KindsMap map[string]reflect.Kind Type reflect.Type }
@TODO 支持 pk, notNull, default 等tag
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
func (*Query) DeleteQuickly ¶
DeleteQuickly 删除Delete,但不返回影响的行数
func (*Query) FindBoolCol ¶
FindBoolCol 查询单个字段值并返回bool值
func (*Query) FindBytesCol ¶
FindBytesCol 查询单个字段值并返回字节Slice
func (*Query) FindFloat32Col ¶
FindFloat32Col 查询某个字段值并返回32位浮点型
func (*Query) FindFloat64Col ¶
FindFloat64Col 查询某个字段值并返回64位浮点型
func (*Query) FindInt64Col ¶
FindInt64Col 查询某个字段值并返回64位整型
func (*Query) FindIntCol ¶
FindIntCol 查询某个字段值并返回整型
func (*Query) FindJSONCol ¶
FindJSONCol 查询单个字段值并返回JSON slice
func (*Query) FindStringCol ¶
FindStringCol 查询单个字段值并返回字符串
func (*Query) InsertOrUpdate ¶
func (this *Query) InsertOrUpdate(insertingValues maps.Map, updatingValues maps.Map) (rowsAffected int64, lastInsertId int64, err error)
InsertOrUpdate 插入或更改 依据要插入的数据中的unique键来决定是插入数据还是替换数据
func (*Query) InsertOrUpdateQuickly ¶
InsertOrUpdateQuickly 插入或更改 依据要插入的数据中的unique键来决定是插入数据还是替换数据
func (*Query) JSONContains ¶
JSONContains JSON包含
func (*Query) Partitions ¶
Partitions 指定分区
func (*Query) ResultExcept ¶
ResultExcept 设置查询不需要返回的字段
type QueryGroup ¶
type QueryGroup struct { Field string Order int }
type QueryOrder ¶
type QueryUseIndex ¶
type QueryUseIndex struct { Keyword string For string Indexes []string }
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (*Result) LastInsertId ¶
func (*Result) RowsAffected ¶
type SQLExecutor ¶
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt SQL语句
type StmtManager ¶
type StmtManager struct {
// contains filtered or unexported fields
}
func NewStmtManager ¶
func NewStmtManager() *StmtManager
func (*StmtManager) Close ¶
func (this *StmtManager) Close() error
func (*StmtManager) CloseId ¶
func (this *StmtManager) CloseId(parentId int64) error
func (*StmtManager) Len ¶
func (this *StmtManager) Len() int
func (*StmtManager) Prepare ¶
func (this *StmtManager) Prepare(preparer sqlPreparer, querySQL string) (*Stmt, error)
Prepare statement
func (*StmtManager) PrepareOnce ¶
func (this *StmtManager) PrepareOnce(preparer sqlPreparer, querySQL string, parentId int64) (resultStmt *Stmt, wasCached bool, err error)
PrepareOnce prepare statement for reuse
func (*StmtManager) SetMaxCount ¶
func (this *StmtManager) SetMaxCount(count int)
type Table ¶
type Table struct { Name string Schema string Fields []*Field Comment string Collation string Engine string Code string // DDL SQL Partitions []*TablePartition // 分区 Indexes []*TableIndex // 索引 MappingName string // 映射的模型名 }
func (*Table) FindFieldWithName ¶
func (*Table) FindIndexWithName ¶
func (this *Table) FindIndexWithName(name string) *TableIndex
func (*Table) FindPartitionWithName ¶
func (this *Table) FindPartitionWithName(name string) *TablePartition
type TableIndex ¶
type TableIndex struct { IsUnique bool Name string ColumnNames []string Type string Comment string }
func (*TableIndex) Definition ¶
func (this *TableIndex) Definition() string
type TablePartition ¶
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}