Documentation
¶
Index ¶
- Constants
- Variables
- func Deref(t reflect.Type) reflect.Type
- func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value
- func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value
- func IsErr(err, target error) bool
- func RoundVar(value interface{}) interface{}
- func ScanAll(ctx context.Context, rows rowsi, dest interface{}, structOnly bool) error
- func StructScan(ctx context.Context, rows rowsi, dest interface{}) error
- func ToStruct(ctx context.Context, rows *sql.Rows, to interface{}) error
- type CommandError
- type Commands
- func (c *Commands) Delete() *Commands
- func (c *Commands) Exec() (sql.Result, error)
- func (c *Commands) For(tableName string) *Commands
- func (c *Commands) Insert(col string, value interface{}) *Commands
- func (c *Commands) NewID(tag string) (int64, error)
- func (q *Commands) OR(cmd string, vars ...string) *Commands
- func (c *Commands) RawSQL(raw string) *Commands
- func (c *Commands) Update(col string, value interface{}) *Commands
- func (c *Commands) Updates(args map[string]interface{}) *Commands
- func (q *Commands) Var(name string, value interface{}) *Commands
- func (q *Commands) Vars(vars map[string]interface{}) *Commands
- func (q *Commands) Where(cmd string, vars ...string) *Commands
- type ConnnetionString
- type Context
- func (c *Context) Begin() error
- func (c *Context) Commit() error
- func (c *Context) NewCommand(ctx context.Context) *Commands
- func (c *Context) NewQuery(ctx context.Context) *Querys
- func (c *Context) PrepareContext(ctx context.Context, commndText string, vars map[string]interface{}, ...) (*sql.Stmt, string, []interface{}, error)
- func (c *Context) Rollback() error
- func (c *Context) TryOpenDb(conn string, maxLifetime time.Duration, maxConns, minConns int) (*sql.DB, error)
- type Database
- type FieldInfo
- type Filter
- type Mapper
- func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value
- func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value
- func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value
- func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int
- func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error
- func (m *Mapper) TypeMap(t reflect.Type) *StructMap
- type NamedStmt
- type Option
- type Querys
- func (q *Querys) Count() (int, error)
- func (q *Querys) Find(objects interface{}) error
- func (q *Querys) First(obj interface{}) error
- func (q *Querys) For(tableName string) *Querys
- func (q *Querys) ForModel(obj interface{}) (*Querys, error)
- func (q *Querys) GroupBy(columns ...string) *Querys
- func (s *Querys) Limit(limit int) *Querys
- func (q *Querys) NewID(tag string) (int64, error)
- func (s *Querys) Offset(offset int) *Querys
- func (q *Querys) OrderBy(columns ...string) *Querys
- func (q *Querys) OrderByDescending(columns ...string) *Querys
- func (q *Querys) Query(scan func(rows *sql.Rows) error) error
- func (q *Querys) QueryRow(scan func(row *sql.Row) error) error
- func (q *Querys) RawSQL(raw string) *Querys
- func (q *Querys) Select(columns ...string) *Querys
- func (q *Querys) SelectModel(object interface{}) *Querys
- func (s *Querys) String() string
- func (q *Querys) Take(pageIndex, pageSize int) *Querys
- func (q *Querys) Var(name string, value interface{}) *Querys
- func (q *Querys) VarIN(name string, values ...interface{}) *Querys
- func (q *Querys) Vars(vars map[string]interface{}) *Querys
- func (q *Querys) Where(cmd string, varNames ...string) *Querys
- func (q *Querys) WhereIN(cmd string, varNames ...string) *Querys
- func (q *Querys) WhereOR(cmd string, vars ...string) *Querys
- func (q *Querys) WhereOrIN(cmd string, varNames ...string) *Querys
- type Row
- type Rows
- type Search
- func (s *Search) IN(query interface{}, values ...interface{}) *Search
- func (s *Search) Limit(limit interface{}) *Search
- func (s *Search) OR(query interface{}, values ...interface{}) *Search
- func (s *Search) Offset(offset interface{}) *Search
- func (s *Search) OrderBy(values ...interface{}) *Search
- func (s *Search) OrderByDescending(values ...interface{}) *Search
- func (s *Search) Where(query interface{}, values ...interface{}) *Search
- type StructMap
- type Tx
- type ValiadtionCallback
- type Validation
Constants ¶
const PagingIndex = "_pi"
PagingIndex 分頁頁碼默認變數名稱
const PagingSize = "_ps"
PagingSize 分頁大小默認變數名稱
Variables ¶
var ( //InstanceID ID生成器实例ID,用于分散式生成时候保持ID唯一,可以使用以下代码做环境设定 // func init(){ // db.InstanceID = types.Atoi(conf.Value("server","id","1"),1) //} InstanceID int64 //Shardings 表ID生器成集合,用于保存各个表ID生成器的当前状态 Shardings = make(map[string]map[string]*shardingId.IdGenerator) )
var ( // export variables ErrDefaultDatabaseMissing = errors.New("Default database is missing in config file") ErrInvalidTransaction = errors.New("Invalid Transaction operation") ErrInvalidObject = errors.New("Invalid Object") ErrTooManySelectedColumns = errors.New("Too Many selected columns") ErrClockMovedBackwards = errors.New("Clock Moved Backwards") ErrInvalidCommand = errors.New("Invalid Command") )
var ErrBadData = errors.New("db: Bad data for column")
ErrBadData 不符合mysql約束的無效數據 1062 : Duplicate entry for PRIMARY/UNIQUE INDEX 1048 : Symbol: ER_BAD_NULL_ERROR; SQLSTATE: 23000 Message: Column '%s' cannot be null 1406 : Symbol: ER_DATA_TOO_LONG; SQLSTATE: 22001 Message: Data too long for column '%s' at row %ld
var ErrKeyExits = errors.New("db: Duplicate entry for key")
ErrKeyExits https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html#error_er_dup_entry
var NameMapper = strings.ToLower
Functions ¶
func FieldByIndexes ¶
FieldByIndexes returns a value for the field given by the struct traversal for the given value.
func FieldByIndexesReadOnly ¶
FieldByIndexesReadOnly returns a value for a particular struct traversal, but is not concerned with allocating nil pointers because the value is going to be used for reading and not setting.
func StructScan ¶
Types ¶
type CommandError ¶
func NewCommandError ¶
func NewCommandError(command string, err error) CommandError
type Commands ¶
type Commands struct {
Querys
// contains filtered or unexported fields
}
Commands a database command session
func (*Commands) OR ¶
EX : query.OR("id ={id} ", "id")
use vars to check the var existed in the var map
type ConnnetionString ¶
type ConnnetionString string
func (*ConnnetionString) String ¶
func (cs *ConnnetionString) String() string
type Context ¶
func (*Context) NewCommand ¶
NewCommand create a new
type Database ¶
Database 数据库操作实例,包含ID生成状态,共享的db连线实例,设定文档
func NewDatabase ¶
NewDatabase 创建数据库操作实例 d := db.NewContext(ctx, db.WithConfig(config))
type FieldInfo ¶
type FieldInfo struct {
Index []int
Path string
Field reflect.StructField
Zero reflect.Value
Name string
Options map[string]string
Embedded bool
Children []*FieldInfo
Parent *FieldInfo
}
A FieldInfo is metadata for a struct field.
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter 過濾器,自動類型轉換,自動索引優先
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper is a general purpose mapper of names to struct fields. A Mapper behaves like most marshallers in the standard library, obeying a field tag for name mapping but also providing a basic transform function.
func NewMapper ¶
NewMapper returns a new mapper using the tagName as its struct field tag. If tagName is the empty string, it is ignored.
func NewMapperFunc ¶
NewMapperFunc returns a new mapper which optionally obeys a field tag and a struct field name mapper func given by f. Tags will take precedence, but for any other field, the mapped name will be f(field.Name)
func NewMapperTagFunc ¶
NewMapperTagFunc returns a new mapper which contains a mapper for field names AND a mapper for tag values. This is useful for tags like json which can have values like "name,omitempty".
func (*Mapper) FieldByName ¶
FieldByName returns a field by its mapped name as a reflect.Value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value if the name is not found.
func (*Mapper) FieldMap ¶
FieldMap returns the mapper's mapping of field names to reflect values. Panics if v's Kind is not Struct, or v is not Indirectable to a struct kind.
func (*Mapper) FieldsByName ¶
FieldsByName returns a slice of values corresponding to the slice of names for the value. Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind. Returns zero Value for each name not found.
func (*Mapper) TraversalsByName ¶
TraversalsByName returns a slice of int slices which represent the struct traversals for each mapped name. Panics if t is not a struct or Indirectable to a struct. Returns empty int slice for each name not found.
func (*Mapper) TraversalsByNameFunc ¶
func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error
TraversalsByNameFunc traverses the mapped names and calls fn with the index of each name and the struct traversal represented by that name. Panics if t is not a struct or Indirectable to a struct. Returns the first error returned by fn or nil.
type Querys ¶
type Querys struct {
// contains filtered or unexported fields
}
Query a datbase query session
func (*Querys) OrderByDescending ¶
func (*Querys) Select ¶
--------------------------------- func (q *Querys) Select(columns ...Column) *Querys {
func (*Querys) SelectModel ¶
func (*Querys) Where ¶
EX : query.Where("id = {id}", "id")
use vars to check the var existed in the var map
func (*Querys) WhereIN ¶
EX : query.WhereIN("id in {id}", "id")
該欄位不能是參與分片的欄位 varNames 指定关联变量名称,如果有任一关联变量名称没有赋值,则这段cmd会被忽略,不参与最终SQL
type Row ¶
type Row struct {
Mapper *Mapper
// contains filtered or unexported fields
}
func (*Row) ColumnTypes ¶
func (r *Row) ColumnTypes() ([]*sql.ColumnType, error)
ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error
type Search ¶
type Search struct {
Unscoped bool
// contains filtered or unexported fields
}
func (*Search) OrderByDescending ¶
type StructMap ¶
type StructMap struct {
Tree *FieldInfo
Index []*FieldInfo
Paths map[string]*FieldInfo
Names map[string]*FieldInfo
}
A StructMap is an index of field metadata for a struct.
func (StructMap) GetByTraversal ¶
GetByTraversal returns a *FieldInfo for a given integer path. It is analogous to reflect.FieldByIndex, but using the cached traversal rather than re-executing the reflect machinery each time.
type ValiadtionCallback ¶
type Validation ¶
type Validation interface {
IsValidate() bool
}