Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultLogger() func(e *Engine)
- func GetErr(err Error, args ...interface{}) error
- func If(condition bool, trueVal, falseVal interface{}) interface{}
- func InArray(needle, hystack interface{}) bool
- func Max(args ...interface{}) int
- func Min(args ...interface{}) int
- func SetDefaultRecover(dr func(err error))
- func StructToMap(obj interface{}) map[string]interface{}
- func WithLockContext(ctx func())
- func WithRLockContext(ctx func())
- func WithRecover(h func(), errFunc ...func(err error))
- type Api
- type BindType
- type Binder
- func (o *Binder) BindParse(prefix string) error
- func (o *Binder) GetBindAll() []Data
- func (o *Binder) GetBindFields() []string
- func (o *Binder) GetBindName() string
- func (o *Binder) GetBindOrigin() interface{}
- func (o *Binder) GetBindPrefix() string
- func (o *Binder) GetBindResult() interface{}
- func (o *Binder) GetBindResultSlice() reflect.Value
- func (o *Binder) GetBindType() BindType
- func (o *Binder) ResetBindResultSlice()
- func (o *Binder) ResetBinder()
- func (o *Binder) SetBindAll(arg []Data)
- func (o *Binder) SetBindFields(arg []string)
- func (o *Binder) SetBindName(arg string)
- func (o *Binder) SetBindOrigin(arg interface{})
- func (o *Binder) SetBindPrefix(arg string)
- func (o *Binder) SetBindResult(arg interface{})
- func (o *Binder) SetBindResultSlice(arg reflect.Value)
- func (o *Binder) SetBindType(arg BindType)
- type Builder
- func (b *Builder) BuildData(operType string) (string, string, string)
- func (b *Builder) BuildData2(operType string) (string, string, string)
- func (b *Builder) BuildDistinct() (dis string)
- func (b *Builder) BuildExecute(operType string) (sqlStr string, args []interface{}, err error)
- func (b *Builder) BuildFields() string
- func (b *Builder) BuildGroup() string
- func (b *Builder) BuildHaving() string
- func (b *Builder) BuildJoin() (s string, err error)
- func (b *Builder) BuildLimit() string
- func (b *Builder) BuildOffset() string
- func (b *Builder) BuildOrder() string
- func (b *Builder) BuildQuery() (sqlStr string, args []interface{}, err error)
- func (b *Builder) BuildTable() string
- func (b *Builder) BuildWhere() (where string, err error)
- func (b *Builder) GetBindValues() []interface{}
- func (b *Builder) GetOperator() []string
- func (b *Builder) GetPlaceholder() (phstr string)
- func (b *Builder) SetBindValues(bv interface{})
- func (b *Builder) SetDriver(dr string) *Builder
- type BuilderClickhouse
- type BuilderMsSql
- type BuilderMysql
- type BuilderOracle
- func (b *BuilderOracle) BuildData(operType string) (string, string, string)
- func (b *BuilderOracle) BuildData2(operType string) (string, string, string)
- func (b *BuilderOracle) BuildDistinct() (dis string)
- func (b *BuilderOracle) BuildExecute(o IOrm, operType string) (sqlStr string, args []interface{}, err error)
- func (b *BuilderOracle) BuildExecuteOra(operType string) (sqlStr string, args []interface{}, err error)
- func (b *BuilderOracle) BuildFields() string
- func (b *BuilderOracle) BuildGroup() string
- func (b *BuilderOracle) BuildHaving() string
- func (b *BuilderOracle) BuildJoin() (s string, err error)
- func (b *BuilderOracle) BuildLimit() string
- func (b *BuilderOracle) BuildOffset() string
- func (b *BuilderOracle) BuildOrder() string
- func (b *BuilderOracle) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
- func (b *BuilderOracle) BuildQueryOra() (sqlStr string, args []interface{}, err error)
- func (b *BuilderOracle) BuildTable() string
- func (b *BuilderOracle) BuildWhere() (where string, err error)
- func (b *BuilderOracle) Clone() IBuilder
- func (b *BuilderOracle) GetOperator() []string
- func (b *BuilderOracle) GetPlaceholder() (phstr string)
- func (b *BuilderOracle) SetDriver(dr string) *BuilderOracle
- type BuilderPostgres
- func (*BuilderPostgres) AddFieldQuotes(field string) string
- func (b *BuilderPostgres) BuildExecute(o IOrm, operationType string) (sqlStr string, args []interface{}, err error)
- func (b *BuilderPostgres) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
- func (b *BuilderPostgres) Clone() IBuilder
- type BuilderSqlite3
- type Config
- type ConfigCluster
- type Data
- type Driver
- type Engine
- func (c *Engine) GetDriver() string
- func (c *Engine) GetExecuteDB() *sql.DB
- func (c *Engine) GetLogger() ILogger
- func (c *Engine) GetPrefix() string
- func (c *Engine) GetQueryDB() *sql.DB
- func (c *Engine) IgnoreName(arg string)
- func (c *Engine) Orm() IOrm
- func (c *Engine) Ping() error
- func (c *Engine) Session() ISession
- func (c *Engine) SetLogger(lg ILogger)
- func (c *Engine) SetPrefix(pre string)
- func (c *Engine) TagName(arg string)
- func (c *Engine) Use(closers ...func(e *Engine))
- type Err
- type Error
- type FieldQuotesDefault
- type IApi
- type IBinder
- type IBuilder
- type IEngine
- type IExecute
- type IFieldQuotes
- type ILogger
- type IOrm
- type IOrmSession
- type IQuery
- type ISession
- type Lang
- type LogLevel
- type LogOption
- type Logger
- type Map
- type Orm
- func (p *Orm) AddField(fields ...string) IOrm
- func (p *Orm) And(args ...interface{}) IOrm
- func (p *Orm) Avg(avg string) (interface{}, error)
- func (p *Orm) Between(needle string, hyStack []interface{}) IOrm
- func (p *Orm) Chunk(limit int, callback func([]Data) error, pointer ...interface{}) (err error)
- func (p *Orm) ChunkStruct(limit int, callback func() error, pointer ...interface{}) (err error)
- func (p *Orm) ClearBindValues()
- func (p *Orm) Close()
- func (p *Orm) Count(args ...string) (int64, error)
- func (p *Orm) CrossJoin(args ...interface{}) IOrm
- func (p *Orm) Data(data interface{}) IOrm
- func (p *Orm) Decrement(args ...interface{}) (int64, error)
- func (p *Orm) Delete() (int64, error)
- func (p *Orm) Distinct() IOrm
- func (p *Orm) ExtraCols(args ...string) IOrm
- func (p *Orm) Field(fields ...string) IOrm
- func (p *Orm) Find(pointer ...interface{}) (result []Data, err error)
- func (p *Orm) First(pointer ...interface{}) (result Data, err error)
- func (p *Orm) Force() IOrm
- func (p *Orm) GetBindValues() []interface{}
- func (p *Orm) GetData() interface{}
- func (p *Orm) GetDistinct() bool
- func (p *Orm) GetDriver() string
- func (p *Orm) GetExtraCols() []string
- func (p *Orm) GetFields() []string
- func (p *Orm) GetForce() bool
- func (p *Orm) GetGroup() string
- func (p *Orm) GetHaving() string
- func (p *Orm) GetISession() ISession
- func (p *Orm) GetJoin() [][]interface{}
- func (p *Orm) GetLimit() int
- func (p *Orm) GetOffset() int
- func (p *Orm) GetOrder() string
- func (p *Orm) GetOrmApi() *Api
- func (p *Orm) GetPessimisticLock() string
- func (p *Orm) GetTable() string
- func (p *Orm) GetWhere() [][]interface{}
- func (p *Orm) Group(group string) IOrm
- func (p *Orm) Having(having string) IOrm
- func (p *Orm) In(needle string, hyStack []interface{}) IOrm
- func (p *Orm) Increment(args ...interface{}) (int64, error)
- func (p *Orm) Insert(data ...interface{}) (int64, error)
- func (p *Orm) InsertGetId(data ...interface{}) (int64, error)
- func (p *Orm) Join(args ...interface{}) IOrm
- func (p *Orm) LeftJoin(args ...interface{}) IOrm
- func (p *Orm) Limit(limit int) IOrm
- func (p *Orm) LockForUpdate() *Orm
- func (p *Orm) Loop(limit int, callback func([]Data) error) (err error)
- func (p *Orm) Max(max string) (interface{}, error)
- func (p *Orm) Min(min string) (interface{}, error)
- func (p *Orm) NotBetween(needle string, hyStack []interface{}) IOrm
- func (p *Orm) NotIn(needle string, hyStack []interface{}) IOrm
- func (p *Orm) NotNull(arg string) IOrm
- func (p *Orm) NotRegexp(arg string, expStr string) IOrm
- func (p *Orm) Null(arg string) IOrm
- func (p *Orm) Offset(offset int) IOrm
- func (p *Orm) Or(args ...interface{}) IOrm
- func (p *Orm) OrBetween(needle string, hyStack []interface{}) IOrm
- func (p *Orm) OrIn(needle string, hyStack []interface{}) IOrm
- func (p *Orm) OrNotBetween(needle string, hyStack []interface{}) IOrm
- func (p *Orm) OrNotIn(needle string, hyStack []interface{}) IOrm
- func (p *Orm) OrNotNull(arg string) IOrm
- func (p *Orm) OrNotRegexp(arg string, expStr string) IOrm
- func (p *Orm) OrNull(arg string) IOrm
- func (p *Orm) OrRegexp(arg string, expStr string) IOrm
- func (p *Orm) Order(order string) IOrm
- func (p *Orm) Page(page int) IOrm
- func (p *Orm) Paginate(page ...int) (res Data, err error)
- func (p *Orm) Pluck(pointer interface{}, field string, fieldKey ...string) (v interface{}, err error)
- func (p *Orm) PluckBak(pointer interface{}, field string, fieldKey ...string) (v interface{}, err error)
- func (p *Orm) Regexp(arg string, expStr string) IOrm
- func (p *Orm) Reset() IOrm
- func (p *Orm) ResetExtraCols() IOrm
- func (p *Orm) ResetTable() IOrm
- func (p *Orm) ResetUnion() IOrm
- func (p *Orm) ResetWhere() IOrm
- func (p *Orm) RightJoin(args ...interface{}) IOrm
- func (p *Orm) Scan(pointer ...interface{}) error
- func (p *Orm) SetBindValues(v interface{})
- func (p *Orm) SetISession(is ISession)
- func (p *Orm) SetWhere(arg [][]interface{})
- func (p *Orm) SharedLock() *Orm
- func (p *Orm) Sql(operationType ...string) (a string, b []interface{}, err error)
- func (p *Orm) Sum(sum string) (interface{}, error)
- func (p *Orm) TX(closers ...func(db IOrm) error) (err error)
- func (p *Orm) Table(tableName interface{}) IOrm
- func (p *Orm) Update(data ...interface{}) (int64, error)
- func (p *Orm) Value(field string) (v interface{}, err error)
- func (p *Orm) ValueBak(field string, pointer ...interface{}) (v interface{}, err error)
- func (p *Orm) Where(args ...interface{}) IOrm
- type Session
- func (s *Session) Begin() (err error)
- func (s *Session) Bind(pointer interface{}) ISession
- func (s *Session) Close()
- func (s *Session) Commit() (err error)
- func (s *Session) Exec(sqlStr string, args ...interface{}) (rowsAffected int64, err error)
- func (s *Session) GetErr() error
- func (s *Session) GetIBinder() IBinder
- func (s *Session) GetIEngine() IEngine
- func (s *Session) GetTableName() (string, error)
- func (s *Session) GetTransaction() bool
- func (s *Session) GetUnion() interface{}
- func (s *Session) LastInsertId() int64
- func (s *Session) LastSql() string
- func (s *Session) Query(sqlStr string, args ...interface{}) (result []Data, err error)
- func (s *Session) ResetBinderResult()
- func (s *Session) Rollback() (err error)
- func (s *Session) SetIBinder(ib IBinder)
- func (s *Session) SetIEngine(ie IEngine)
- func (s *Session) SetTransaction(b bool)
- func (s *Session) SetUnion(u interface{})
- func (s *Session) TX(closers ...func(ses ISession) error) (err error)
- type StructEngine
- func (s *StructEngine) AppendFields(arg interface{})
- func (s *StructEngine) AppendResult(arg map[string]interface{})
- func (s *StructEngine) GetFields() []interface{}
- func (s *StructEngine) GetResult() []map[string]interface{}
- func (s *StructEngine) GetStructFields(data interface{}) []interface{}
- func (s *StructEngine) GetTagIgnoreName() string
- func (s *StructEngine) GetTagName() string
- func (s *StructEngine) SetExtraCols(args []string) *StructEngine
- func (s *StructEngine) SetFields(arg []interface{})
- func (s *StructEngine) SetResult(arg []map[string]interface{})
- func (s *StructEngine) SetTagIgnoreName(arg string) *StructEngine
- func (s *StructEngine) SetTagName(arg string) *StructEngine
- func (s *StructEngine) StructContent2Map(data interface{}) []map[string]interface{}
Constants ¶
const (
// DriverClickhouse ...
DriverClickhouse = "clickhouse"
)
const (
// DriverMsSql ...
DriverMsSql = "mssql"
)
const (
// DriverMysql ...
DriverMysql = "mysql"
)
const (
// DriverOracle ...
DriverOracle = "oci8"
)
const (
// DriverPostgres ...
DriverPostgres = "postgres"
)
const (
// DriverSqlite3 ...
DriverSqlite3 = "sqlite3"
)
Variables ¶
var BindString = map[BindType]string{ ObjectStruct: "OBJECT_STRUCT", ObjectStructSlice: "OBJECT_STRUCT_SLICE", ObjectMap: "OBJECT_MAP", ObjectMapSlice: "OBJECT_MAP_SLICE", ObjectString: "OBJECT_STRING", ObjectMapT: "OBJECT_MAP_T", ObjectMapSliceT: "OBJECT_MAP_SLICE_T", ObjectNil: "OBJECT_NIL", }
BindString ...
var Ignore = "-"
Ignore ...
var TagName = "orm"
TagName ...
Functions ¶
func If ¶
func If(condition bool, trueVal, falseVal interface{}) interface{}
If : ternary operator (三元运算) condition:比较运算 trueVal:运算结果为真时的值 falseVal:运算结果为假时的值 return: 由于不知道传入值的类型, 所有, 必须在接收结果时, 指定对应的值类型
func SetDefaultRecover ¶
func SetDefaultRecover(dr func(err error))
func WithLockContext ¶
func WithLockContext(ctx func())
func WithRLockContext ¶
func WithRLockContext(ctx func())
func WithRecover ¶
func WithRecover(h func(), errFunc ...func(err error))
Types ¶
type BindType ¶
type BindType int
BindType ...
const ( // ObjectStruct 结构体 一条数据 (struct) ObjectStruct BindType = iota // ObjectStructSlice 结构体 多条数据 ([]struct) ObjectStructSlice // ObjectMap map 一条数据 (map[string]interface{}) ObjectMap // ObjectMapSlice map 多条数据 ([]map[string]interface{}) ObjectMapSlice // ObjectString 非结构体 表名字符串 ("users") ObjectString // ObjectMapT map 一条数据 (map[string]t.Type) ObjectMapT // ObjectMapSliceT map 多条数据 ([]map[string]t.Type) ObjectMapSliceT // ObjectNil 默认没有传入任何绑定对象,一般用于query直接返回 ObjectNil )
type Binder ¶
type Binder struct { // Bind是指传入的对象 [slice]map,[slice]struct // 传入的原始对象 BindOrigin interface{} //BindOriginTableName []string // 解析出来的对象名字, 或者指定的method(TableName)获取到的名字 BindName string // 一条结果的反射对象 BindResult interface{} // 多条 BindResultSlice reflect.Value // 传入结构体解析出来的字段 BindFields []string // 传入的对象类型判定 BindType BindType // 出入传入得是非slice对象, 则只需要取一条, 取多了也是浪费 BindLimit int BindPrefix string // 多条map结果,传入的是string table时 BindAll []Data }
Binder 结果集绑定模块, 所有的返回结果集都在这里。
func (*Binder) GetBindResultSlice ¶
GetBindResultSlice ...
func (*Binder) ResetBindResultSlice ¶
func (o *Binder) ResetBindResultSlice()
ResetBindResultSlice ...
func (*Binder) SetBindResultSlice ¶
SetBindResultSlice ...
type Builder ¶
type Builder struct { IOrm // contains filtered or unexported fields }
BuilderDefault 默认构造器, 其他的可以继承这个, 重写方法即可。
构建终极执行的sql模块, 可以构建任何数据库的sql, 但要符合database/sql包的接口。
func NewBuilderDefault ¶
NewBuilderDefault 初始化一个 默认 Builder。
func (*Builder) BuildData2 ¶
BuildData2 ...
func (*Builder) BuildExecute ¶
BuilderDefault.BuildExecut : build execute query string
func (*Builder) BuildQuery ¶
BuildQuery 构造query
func (*Builder) BuildWhere ¶
BuildWhere ...
func (*Builder) GetPlaceholder ¶
GetPlaceholder 获取占位符
type BuilderClickhouse ¶
type BuilderClickhouse struct { FieldQuotesDefault // contains filtered or unexported fields }
BuilderClickhouse ...
func (*BuilderClickhouse) BuildExecute ¶
func (b *BuilderClickhouse) BuildExecute(o IOrm, operationType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderClickhouse) BuildQuery ¶
func (b *BuilderClickhouse) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
BuildQuery : build query sql string
type BuilderMsSql ¶
type BuilderMsSql struct { FieldQuotesDefault // contains filtered or unexported fields }
BuilderMsSql ...
func (*BuilderMsSql) BuildExecute ¶
func (b *BuilderMsSql) BuildExecute(o IOrm, operationType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderMsSql) BuildQuery ¶
func (b *BuilderMsSql) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
BuildQuery : build query sql string
type BuilderMysql ¶
type BuilderMysql struct { FieldQuotesDefault // contains filtered or unexported fields }
BuilderMysql ...
func (*BuilderMysql) BuildExecute ¶
func (b *BuilderMysql) BuildExecute(o IOrm, operationType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderMysql) BuildQuery ¶
func (b *BuilderMysql) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
BuildQuery : build query sql string
type BuilderOracle ¶
type BuilderOracle struct { FieldQuotesDefault Builder }
BuilderOracle ...
func (*BuilderOracle) BuildData ¶
func (b *BuilderOracle) BuildData(operType string) (string, string, string)
BuildData ...
func (*BuilderOracle) BuildData2 ¶
func (b *BuilderOracle) BuildData2(operType string) (string, string, string)
BuildData2 ...
func (*BuilderOracle) BuildDistinct ¶
func (b *BuilderOracle) BuildDistinct() (dis string)
BuildDistinct ...
func (*BuilderOracle) BuildExecute ¶
func (b *BuilderOracle) BuildExecute(o IOrm, operType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderOracle) BuildExecuteOra ¶
func (b *BuilderOracle) BuildExecuteOra(operType string) (sqlStr string, args []interface{}, err error)
BuildExecuteOra ...
func (*BuilderOracle) BuildJoin ¶
func (b *BuilderOracle) BuildJoin() (s string, err error)
BuildJoin ...
func (*BuilderOracle) BuildQuery ¶
func (b *BuilderOracle) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
实现接口 BuildQuery : build query sql string
func (*BuilderOracle) BuildQueryOra ¶
func (b *BuilderOracle) BuildQueryOra() (sqlStr string, args []interface{}, err error)
BuildQueryOra ...
func (*BuilderOracle) BuildWhere ¶
func (b *BuilderOracle) BuildWhere() (where string, err error)
BuildWhere ...
func (*BuilderOracle) GetPlaceholder ¶
func (b *BuilderOracle) GetPlaceholder() (phstr string)
GetPlaceholder 获取占位符
func (*BuilderOracle) SetDriver ¶
func (b *BuilderOracle) SetDriver(dr string) *BuilderOracle
SetDriver 设置驱动, 方便获取占位符使用
type BuilderPostgres ¶
type BuilderPostgres struct {
// contains filtered or unexported fields
}
BuilderPostgres ...
func (*BuilderPostgres) AddFieldQuotes ¶
func (*BuilderPostgres) AddFieldQuotes(field string) string
func (*BuilderPostgres) BuildExecute ¶
func (b *BuilderPostgres) BuildExecute(o IOrm, operationType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderPostgres) BuildQuery ¶
func (b *BuilderPostgres) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
BuildQuery : build query sql string
type BuilderSqlite3 ¶
type BuilderSqlite3 struct { FieldQuotesDefault // contains filtered or unexported fields }
BuilderSqlite3 ...
func (*BuilderSqlite3) BuildExecute ¶
func (b *BuilderSqlite3) BuildExecute(o IOrm, operType string) (sqlStr string, args []interface{}, err error)
BuildExecute : build execute sql string
func (*BuilderSqlite3) BuildQuery ¶
func (b *BuilderSqlite3) BuildQuery(o IOrm) (sqlStr string, args []interface{}, err error)
BuildQuery : build query sql string
type Config ¶
type Config struct { Driver string `json:"driver"` // 驱动: mysql/sqlite3/oracle/mssql/postgres/clickhouse, 如果集群配置了驱动, 这里可以省略 // mysql 示例: // root:root@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=true Dsn string `json:"dsn"` // 数据库链接 SetMaxOpenConns int `json:"setMaxOpenConns"` // (连接池)最大打开的连接数,默认值为0表示不限制 SetMaxIdleConns int `json:"setMaxIdleConns"` // (连接池)闲置的连接数, 默认0 Prefix string `json:"prefix"` // 表前缀, 如果集群配置了前缀, 这里可以省略 }
Config ...
type ConfigCluster ¶
type ConfigCluster struct { Master []Config // 主数据库配置节点 Slave []Config // 从数据库配置节点 Driver string // 驱动 Prefix string // 前缀 }
ConfigCluster ...
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver (原 BuilderDriver )数据库驱动模块, 被 Engine 和 Builder 依赖, 根据驱动来搞事情。
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine 初始化配置模块, 可以全局保存并复用。
func (*Engine) GetExecuteDB ¶
GetExecuteDB : 获取一个主库用来做查询之外的操作
func (*Engine) IgnoreName ¶
IgnoreName 自定义结构体对应的orm忽略字段名字,默认-
func (*Engine) Orm ¶
NewOrm 获取 DB 实例。
这是一个语法糖, 为了方便使用 engine.NewOrm()添加的。 添加后会让 Engine 和 DB 耦合, 如果不想耦合, 就删掉此方法。 删掉这个方法后,可以使用 orm.NewOrm(orm.NewSession(orm.IEngine)) 通过 orm.ISession 依赖注入的方式, 达到解耦的目的。
type FieldQuotesDefault ¶
type FieldQuotesDefault struct{}
func (FieldQuotesDefault) AddFieldQuotes ¶
func (FieldQuotesDefault) AddFieldQuotes(field string) string
type IApi ¶
type IApi interface { GetTable() string GetFields() []string SetWhere(arg [][]interface{}) GetWhere() [][]interface{} GetOrder() string GetLimit() int GetOffset() int GetJoin() [][]interface{} GetDistinct() bool GetGroup() string GetHaving() string GetData() interface{} ExtraCols(args ...string) IOrm ResetExtraCols() IOrm GetExtraCols() []string GetPessimisticLock() string }
IApi 各个驱动实现这个接口。
type IBinder ¶
type IBinder interface { SetBindOrigin(arg interface{}) GetBindOrigin() interface{} SetBindName(arg string) GetBindName() string SetBindResult(arg interface{}) GetBindResult() interface{} SetBindResultSlice(arg reflect.Value) GetBindResultSlice() reflect.Value SetBindFields(arg []string) GetBindFields() []string SetBindType(arg BindType) GetBindType() BindType BindParse(prefix string) error SetBindPrefix(arg string) GetBindPrefix() string ResetBindResultSlice() SetBindAll(arg []Data) GetBindAll() []Data ResetBinder() }
IBinder 数据绑定对象接口,是 Binder 的接口。
type IBuilder ¶
type IBuilder interface { IFieldQuotes BuildQuery(orm IOrm) (sqlStr string, args []interface{}, err error) BuildExecute(orm IOrm, operType string) (sqlStr string, args []interface{}, err error) Clone() IBuilder }
IBuilder ...
type IEngine ¶
type IEngine interface { GetExecuteDB() *sql.DB GetQueryDB() *sql.DB GetPrefix() (pre string) SetLogger(lg ILogger) GetLogger() ILogger GetDriver() string }
IEngine ...
type IExecute ¶
type IExecute interface { GetForce() bool Insert(data ...interface{}) (int64, error) InsertGetId(data ...interface{}) (int64, error) Update(data ...interface{}) (int64, error) Delete() (int64, error) // increment,decrement // 在操作过程中你还可以指定额外的列进行更新: Increment(args ...interface{}) (int64, error) Decrement(args ...interface{}) (int64, error) Force() IOrm }
IExecute ... Orm的执行接口。
type IFieldQuotes ¶
IFieldQuotes 给系统关键词冲突的字段加引号,如: mysql是反引号, pg是双引号
type ILogger ¶
type ILogger interface { Sql(sqlStr string, runtime time.Duration) Slow(sqlStr string, runtime time.Duration) Error(msg string) EnableSqlLog() bool EnableErrorLog() bool EnableSlowLog() float64 }
ILogger ...
type IOrm ¶
type IOrm interface { IApi IQuery IExecute IOrmSession Close() Sql(operationType ...string) (string, []interface{}, error) // 原(BuildSql)构建 Sql 指令。即: CRUD。参数<operationType>只能是(select, insert, update, delete) Table(tab interface{}) IOrm // fields=select Field(fields ...string) IOrm AddField(fields ...string) IOrm // distinct 方法允许你强制查询返回不重复的结果集: Distinct() IOrm Data(data interface{}) IOrm Group(group string) IOrm Having(having string) IOrm Order(order string) IOrm Limit(limit int) IOrm Offset(offset int) IOrm Page(page int) IOrm // join(=innerJoin),leftJoin,rightJoin,crossJoin Join(args ...interface{}) IOrm LeftJoin(args ...interface{}) IOrm RightJoin(args ...interface{}) IOrm CrossJoin(args ...interface{}) IOrm // ` Where`,` Or`,` Null / NotNull`,` In / NotIn / OrIn / OrNotIn`,` Between / OrBetween / OrNotBetween / OrNotBetween` Where(args ...interface{}) IOrm And(args ...interface{}) IOrm Or(args ...interface{}) IOrm Null(arg string) IOrm OrNull(arg string) IOrm NotNull(arg string) IOrm OrNotNull(arg string) IOrm Regexp(arg string, expstr string) IOrm OrRegexp(arg string, expstr string) IOrm NotRegexp(arg string, expstr string) IOrm OrNotRegexp(arg string, expstr string) IOrm In(needle string, hystack []interface{}) IOrm OrIn(needle string, hystack []interface{}) IOrm NotIn(needle string, hystack []interface{}) IOrm OrNotIn(needle string, hystack []interface{}) IOrm Between(needle string, hystack []interface{}) IOrm OrBetween(needle string, hystack []interface{}) IOrm NotBetween(needle string, hystack []interface{}) IOrm OrNotBetween(needle string, hystack []interface{}) IOrm GetDriver() string SetBindValues(v interface{}) GetBindValues() []interface{} ClearBindValues() TX(closers ...func(db IOrm) error) (err error) //事务 Reset() IOrm ResetTable() IOrm ResetWhere() IOrm GetISession() ISession GetOrmApi() *Api // sharedLock(lock in share mode) 不会阻塞其它事务读取被锁定行记录的值 SharedLock() *Orm // 此外你还可以使用 lockForUpdate 方法。“for update”锁避免选择行被其它共享锁修改或删除: // 会阻塞其他锁定性读对锁定行的读取(非锁定性读仍然可以读取这些记录,lock in share mode 和 for update 都是锁定性读) LockForUpdate() *Orm }
IOrm 是 Orm (对象关系映射)模块的接口。直接暴露给用户的CRUD等一系列API。
type IOrmSession ¶
type IOrmSession interface { Begin() (err error) Rollback() (err error) Commit() (err error) Query(sqlStr string, args ...interface{}) ([]Data, error) Exec(sqlStr string, args ...interface{}) (int64, error) LastInsertId() int64 LastSql() string GetIBinder() IBinder SetUnion(u interface{}) GetUnion() interface{} }
IOrmSession ...
type IQuery ¶
type IQuery interface { // 参数<pointer>为空时,选择查询一条或多条数据并绑定到传入对象上。 // 参数<pointer>非空时,查询一条或多条数据并绑定到 pointer上。 // 当绑定对象传入的是string类型时, 返回多条结果集, 需要使用 Find() 来获取最终结果 Scan(pointer ...interface{}) error // 获取一条结果并返回, 只有当传入的table对象是字符串时生效 First(pointer ...interface{}) (Data, error) // 获取多条结果并返回, 只有当传入的table对象是字符串时生效 Find(pointer ...interface{}) ([]Data, error) // 如果你不需要完整的一行,可以使用 value 方法从结果中获取单个值,该方法会直接返回指定列的值: Value(field string) (v interface{}, err error) // 如果想要获取包含单个列值的数组,可以使用 pluck 方法 // 还可以在返回数组中为列值指定自定义键(该自定义键必须是该表的其它字段列名,否则会报错) Pluck(pointer interface{}, field string, fieldKey ...string) (v interface{}, err error) // 查询构建器还提供了多个聚合方法,如count, max, min, avg 和 sum,你可以在构造查询之后调用这些方法: Count(args ...string) (int64, error) Sum(sum string) (interface{}, error) Avg(avg string) (interface{}, error) Max(max string) (interface{}, error) Min(min string) (interface{}, error) // 分页, 返回分页需要的基本数据 Paginate(page ...int) (res Data, err error) // 组块结果集 // 如果你需要处理成千上万或者更多条数据库记录,可以考虑使用 chunk 方法,该方法一次获取结果集的一小块, // 然后传递每一小块数据到闭包函数进行处理,该方法在编写处理大量数据库记录的 Artisan 命令的时候非常有用。 // 例如,我们可以将处理全部 users 表数据分割成一次处理 100 条记录的小组块 // 你可以通过从闭包函数中返回 err 来终止组块的运行 Chunk(limit int, callback func([]Data) error, pointer ...interface{}) (err error) // 跟Chunk类似,只不过callback的是传入的结构体 ChunkStruct(limit int, callback func() error, pointer ...interface{}) (err error) Loop(limit int, callback func([]Data) error) (err error) }
IQuery 查询接口。
type ISession ¶
type ISession interface { Close() Bind(bind interface{}) ISession Begin() (err error) Rollback() (err error) Commit() (err error) TX(closer ...func(session ISession) error) (err error) Query(sqlStr string, args ...interface{}) ([]Data, error) // 原生查询SQL Exec(sqlStr string, args ...interface{}) (int64, error) // 原生执行SQL GetIEngine() IEngine LastInsertId() int64 LastSql() string GetTableName() (string, error) SetIBinder(ib IBinder) GetIBinder() IBinder SetUnion(u interface{}) GetUnion() interface{} SetTransaction(b bool) GetTransaction() bool GetBindAll() []Data GetErr() error }
ISession 真正操作数据库底层的接口, 是 Session 的接口。
type LogOption ¶
type LogOption struct { FilePath string EnableSqlLog bool // 是否记录慢查询, 默认0s, 不记录, 设置记录的时间阀值, 比如 1, 则表示超过1s的都记录 EnableSlowLog float64 EnableErrorLog bool }
LogOption ...
type Orm ¶
Orm 对象关系映射模块, 所有的orm操作, 都在这里完成。
func (*Orm) Chunk ¶
Chunk : 分块处理数据,当要处理很多数据的时候, 我不需要知道具体是多少数据, 我只需要每次取limit条数据, 然后不断的增加offset去取更多数据, 从而达到分块处理更多数据的目的 TODO 后续增加 gorotine 支持, 提高批量数据处理效率, 预计需要增加获取更多链接的支持
func (*Orm) ChunkStruct ¶
ChunkStruct : 同Chunk,只不过不用返回map, 而是绑定数据到传入的对象上 这里一定要传入绑定struct
func (*Orm) Decrement ¶
Decrement : auto Decrement -1 default we can define step (such as 2, 3, 6 ...) if give the second params
func (*Orm) Increment ¶
Increment : 默认 自动增量+1 we can define step (such as 2, 3, 6 ...) if give the second params we can use this method as decrement with the third param as "-" orm.Increment("top") , orm.Increment("top", 2, "-")=orm.Decrement("top",2)
func (*Orm) InsertGetId ¶
insertGetId : 插入数据并获取该数据对应的ID
func (*Orm) LockForUpdate ¶
LockForUpdate select * from xxx for update
func (*Orm) Loop ¶
Loop : 同chunk, 不过, 这个是循环的取前limit条数据, 为什么是循环取这些数据呢 因为, 我们考虑到一种情况, 那就是where条件如果刚好是要修改的值, 那么最后的修改结果因为offset的原因, 只会修改一半, 比如: DB().Where("age", 18) ===> DB().Data(gorose.Data{"age":19}).Where().Update()
func (*Orm) NotBetween ¶
NotBetween ...
func (*Orm) OrNotBetween ¶
OrNotBetween ...
func (*Orm) Paginate ¶
Paginate 自动分页 @param limit 每页展示数量 @param current_page 当前第几页, 从1开始 以下是laravel的Paginate返回示例
{ "total": 50, "per_page": 15, "current_page": 1, "lastPage": 4, "first_page_url": "http://laravel.app?page=1", "lastPage_url": "http://laravel.app?page=4", "nextPage_url": "http://laravel.app?page=2", "prevPage_url": null, "path": "http://laravel.app", "from": 1, "to": 15, "data":[ { // Result Object }, { // Result Object } ] }
func (*Orm) Pluck ¶
func (p *Orm) Pluck(pointer interface{}, field string, fieldKey ...string) (v interface{}, err error)
Pluck 获取一列数据, 第二个字段可以指定另一个字段的值作为这一列数据的key
func (*Orm) PluckBak ¶
func (p *Orm) PluckBak(pointer interface{}, field string, fieldKey ...string) (v interface{}, err error)
PluckBak ...
func (*Orm) Scan ¶
Scan : (原Select)
参数<pointer>为空时,选择查询一条或多条数据并绑定到传入对象上。 参数<pointer>非空时,查询一条或多条数据并绑定到 pointer上。 当绑定对象传入的是string类型时, 返回多条结果集, 需要使用 Find() 来获取最终结果。
func (*Orm) SharedLock ¶
SharedLock 共享锁 select * from xxx lock in share mode
func (*Orm) Sql ¶
Sql (原BuildSql),构建 Sql 指令。即: CRUD
参数<operationType>只能是(select, insert, update, delete)
type Session ¶
Session 真正操作数据库底层模块, 所有的操作, 最终都会走到这里来获取或修改数据。
func (*Session) Bind ¶
Bind : 传入绑定结果的对象, 参数一为对象, 可以是 struct, gorose.MapRow 或对应的切片
如果是做非query操作,第一个参数也可以仅仅指定为字符串表名
func (*Session) GetTableName ¶
GetTableName 获取解析后的名字, 提供给 DB 使用 为什么要在这里重复添加该方法, 而不是直接继承 IBinder 的方法呢? 是因为, 这里涉及到表前缀的问题, 只能通过 Session 来传递, 所以 IDB 就可以选择直接继承
func (*Session) GetTransaction ¶
GetTransaction 提供给 DB 使用的, 方便reset操作
type StructEngine ¶
type StructEngine struct { Fields []interface{} Result []map[string]interface{} TagName string TagIgnoreName string ExtraCols []string }
func (*StructEngine) AppendFields ¶
func (s *StructEngine) AppendFields(arg interface{})
func (*StructEngine) AppendResult ¶
func (s *StructEngine) AppendResult(arg map[string]interface{})
func (*StructEngine) GetFields ¶
func (s *StructEngine) GetFields() []interface{}
func (*StructEngine) GetResult ¶
func (s *StructEngine) GetResult() []map[string]interface{}
func (*StructEngine) GetStructFields ¶
func (s *StructEngine) GetStructFields(data interface{}) []interface{}
func (*StructEngine) GetTagIgnoreName ¶
func (s *StructEngine) GetTagIgnoreName() string
func (*StructEngine) GetTagName ¶
func (s *StructEngine) GetTagName() string
func (*StructEngine) SetExtraCols ¶
func (s *StructEngine) SetExtraCols(args []string) *StructEngine
func (*StructEngine) SetFields ¶
func (s *StructEngine) SetFields(arg []interface{})
func (*StructEngine) SetResult ¶
func (s *StructEngine) SetResult(arg []map[string]interface{})
func (*StructEngine) SetTagIgnoreName ¶
func (s *StructEngine) SetTagIgnoreName(arg string) *StructEngine
func (*StructEngine) SetTagName ¶
func (s *StructEngine) SetTagName(arg string) *StructEngine
func (*StructEngine) StructContent2Map ¶
func (s *StructEngine) StructContent2Map(data interface{}) []map[string]interface{}
Source Files ¶
- binder.go
- binder_interface.go
- builder.go
- builder_clickhouse.go
- builder_driver.go
- builder_interface.go
- builder_mssql.go
- builder_mysql.go
- builder_oracle.go
- builder_postgres.go
- builder_sqlite3.go
- config.go
- engin.go
- engin_interface.go
- err.go
- logger.go
- logger_interface.go
- orm.go
- orm_api.go
- orm_api_interface.go
- orm_execute.go
- orm_execute_interface.go
- orm_interface.go
- orm_query.go
- orm_query_interface.go
- orm_session_interface.go
- session.go
- session_interface.go
- util.go
- util_helper.go
- util_struct_engine.go