Documentation
¶
Index ¶
- Variables
- func Connect(configs map[string]*Config) (err error)
- func Exec(sqlStr string, args ...interface{}) (sql.Result, error)
- func Expr(expression string, args ...interface{}) *expr
- func GetDB(name ...string) *sqlx.DB
- func Import(f string) ([]sql.Result, error)
- func IsZero(val reflect.Value) bool
- func JsonObject(value interface{}) (json.RawMessage, error)
- func List() map[string]*sqlx.DB
- func QueryRowX(querySql string, args ...interface{}) *sqlx.Row
- func QueryX(querySql string, args ...interface{}) (*sqlx.Rows, error)
- func RegisterDialect(name string, dialect Dialect)
- func RelationAll(wrapper *ModelWrapper, db *DB, data interface{}) error
- func RelationOne(wrapper *ModelWrapper, db *DB, data interface{}) error
- func Select(pointer interface{}, querySql string, args ...interface{}) error
- func SetDefaultLink(db string)
- func SetLogger(l ILogger)
- func SetLogging(print bool)
- func Take(pointer interface{}, querySql string, args ...interface{}) error
- func Tx(fn func(tx *DB) error) error
- func TxCtx(ctx context.Context, fn func(ctx context.Context, tx *DB) error) error
- type BuildFunc
- type Builder
- func (b *Builder) Cols(fields string) *Builder
- func (b *Builder) Count(zeroValues ...string) (num int64, err error)
- func (b *Builder) Delete(zeroValues ...string) (affected int64, err error)
- func (b *Builder) ForceIndex(i string) *Builder
- func (b *Builder) Hint(hint string) *Builder
- func (b *Builder) Insert() (lastInsertId int64, err error)
- func (b *Builder) Limit(i int) *Builder
- func (b *Builder) Model(model interface{}) *Builder
- func (b *Builder) Offset(i int) *Builder
- func (b *Builder) OrderBy(str string) *Builder
- func (b *Builder) Relation(fieldName string, fn BuildFunc) *Builder
- func (b *Builder) Select() (err error)
- func (b *Builder) ShowSQL() *Builder
- func (b *Builder) Take(zeroValues ...string) (err error)
- func (b *Builder) Update(zeroValues ...string) (affected int64, err error)
- func (b *Builder) Where(str string, args ...interface{}) *Builder
- type Config
- type DB
- func (w *DB) Begin() (*DB, error)
- func (w *DB) Commit() error
- func (w *DB) Ctx(ctx context.Context) *Builder
- func (w *DB) DriverName() string
- func (w *DB) Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
- func (w *DB) ExecN(query string, args interface{}) (result sql.Result, err error)
- func (w *DB) Import(fileName string) ([]sql.Result, error)
- func (w *DB) Model(pointer interface{}) *Builder
- func (w *DB) PrepareX(query string) (*sqlx.Stmt, error)
- func (w *DB) QueryRowX(querySql string, args ...interface{}) (rows *sqlx.Row)
- func (w *DB) QueryX(querySql string, args ...interface{}) (rows *sqlx.Rows, err error)
- func (w *DB) Rebind(query string) string
- func (w *DB) Relation(name string, fn BuildFunc) *DB
- func (w *DB) Rollback() error
- func (w *DB) Select(pointer interface{}, querySql string, args ...interface{}) (err error)
- func (w *DB) Table(tableName string) *Mapper
- func (w *DB) Take(pointer interface{}, querySql string, args ...interface{}) (err error)
- func (w *DB) Tx(fn func(w *DB) error) (err error)
- func (w *DB) TxCtx(ctx context.Context, fn func(ctx context.Context, tx *DB) error) (err error)
- type Dialect
- type Hook
- type ILogger
- type IModel
- type ISql
- type JSONText
- func (j JSONText) MarshalBinary() ([]byte, error)
- func (j JSONText) MarshalJSON() ([]byte, error)
- func (j *JSONText) Scan(src interface{}) error
- func (j JSONText) String() string
- func (j *JSONText) Unmarshal(v interface{}) error
- func (j *JSONText) UnmarshalBinary(data []byte) error
- func (j *JSONText) UnmarshalJSON(data []byte) error
- func (j JSONText) Value() (driver.Value, error)
- type Mapper
- func (m *Mapper) Count() (num int64, err error)
- func (m *Mapper) Delete() (affected int64, err error)
- func (m *Mapper) Insert(data map[string]interface{}) (lastInsertId int64, err error)
- func (m *Mapper) ShowSQL() *Mapper
- func (m *Mapper) Update(data map[string]interface{}) (affected int64, err error)
- func (m *Mapper) Where(str string, args ...interface{}) *Mapper
- type ModelWrapper
- type ModelWrapperFactory
- type QueryStatus
- type ReflectMapper
- type SqlBuilder
Constants ¶
This section is empty.
Variables ¶
var ( AutoCreateTimeFields = []string{ "create_time", "create_at", "created_at", "update_time", "update_at", "updated_at", } AutoUpdateTimeFields = []string{ "update_time", "update_at", "updated_at", } )
var (
FatalExit = true //如果该值为true,则当前数据库退出。
)
Functions ¶
func Expr ¶
func Expr(expression string, args ...interface{}) *expr
Expr 生成原始SQL表达式,例如:
sq.Table("user").Update(map[string]interface{}{"price", sq.Expr("price * ? + ?", 2, 100)})
func JsonObject ¶
func JsonObject(value interface{}) (json.RawMessage, error)
func RelationAll ¶
func RelationAll(wrapper *ModelWrapper, db *DB, data interface{}) error
RelationAll is gets the associated relational data for multiple pieces of data
func RelationOne ¶
func RelationOne(wrapper *ModelWrapper, db *DB, data interface{}) error
RelationOne is get the associated relational data for a single piece of data
Types ¶
type Builder ¶
type Builder struct { SqlBuilder // contains filtered or unexported fields }
Builder 模型结构体
func (*Builder) Delete ¶
Delete 删除一条记录。参数<zeroValues>即使是0值,也会被强制执行。
例如: sq.Model(&User{Id:1}).Delete()。
func (*Builder) Take ¶
Take 查询一条记录。参数<zeroValues>即使是0值,也会被强制执行。
例如: user := &Users{Id:1,Status:0} sq.Model(&user).Take("status")
type Config ¶
type Config struct { Driver string `toml:"driver" json:"driver"` DSN string `toml:"dsn" json:"dsn"` MaxOpenConns int `toml:"max_open_conns" json:"max_open_conns"` MaxIdleConns int `toml:"max_idle_conns" json:"max_idle_conns"` MaxLifetime int `toml:"max_lefttime" json:"max_lefttime"` Enable bool `toml:"enable" json:"enable"` ShowSql bool `toml:"show_sql" json:"show_sql"` // 是否显示SQL语句,ture:显示,false:不显示。 }
Config 是数据库连接配置。
type DB ¶
type DB struct { RelationMap map[string]BuildFunc // // 用来构造一个 Builder 对象的map容器。 // contains filtered or unexported fields }
func (*DB) Ctx ¶
Model database handler from to struct with context for example: sq.Use("db2").Ctx(ctx).Model(&users{})
func (*DB) QueryRowX ¶
QueryRowX 查询一条记录。
参数 <querySql> 是具体的SQL语句;参数 <args> 是SQL语句需要的参数。 注意: 返回的是 *sqlx.Row 而不是 *sql.Row
func (*DB) QueryX ¶
QueryX 查询多条记录。
参数 <querySql> 是具体的SQL语句;参数 <args> 是SQL语句需要的参数。 注意: 返回的是 *sqlx.Rows 而不是 *sql.Rows
func (*DB) Select ¶
Select 快捷方式,查询多条记录,并将查询结果扫描进 <pointer>。
参数 <querySql> 是具体的SQL语句;参数 <args> 是SQL语句需要的参数。
func (*DB) Table ¶
Table database handler from to table name
for example: sq.Use("db2").Table("users")
type Dialect ¶
type Dialect interface { GetName() string // GetName 放回当前正在使用的方言(例如:mysql,mssql...) Quote(key string) string // Quote 通过使用保留字作为字段名来对字段名加引号,以避免SQL解析异常 }
Dialect 处理不同SQL数据库的方言。
type ILogger ¶
type ILogger interface {
Printf(format string, v ...interface{})
}
ILogger 表示一个日志收集器. 您可以将日志收集器传递给 sq.SetLogger(myCollector),以使其在执行查询后收集QueryStatus消息。
type ISql ¶
type ISql interface { // Query 参数<querySql>是查询类的SQL语句,<args>是SQL语句的参数。 QueryX(querySql string, args ...interface{}) (*sqlx.Rows, error) // QueryRow 参数<querySql>是查询类的SQL语句,<args>是SQL语句的参数。 QueryRowX(querySql string, args ...interface{}) *sqlx.Row // Take 查询一条记录,并将结果扫描进 <pointer> // 参数<pointer>可以是struct/*struct。 // 参数<querySql>是查询类的SQL语句,<args>是SQL语句的参数。 Take(pointer interface{}, querySql string, args ...interface{}) error // Select 查询多条记录,并将查询结果扫描进 <pointer> // 参数<pointer>可以是[]struct/*[]struct。 // 参数<querySql>是查询类的SQL语句,<args>是SQL语句的参数。 Select(pointer interface{}, querySql string, args ...interface{}) error // Exec 增、删、改操作的接口。 // 参数<querySql>是查询类的SQL语句,<args>是SQL语句的参数。 Exec(querySql string, args ...interface{}) (sql.Result, error) PrepareX(query string) (*sqlx.Stmt, error) // Rebind 绑定 querySql 语句。 Rebind(querySql string) string // ExecN(query string, arg interface{}) (sql.Result, error) // DriverName 获取当前处于连接状态的 *sql.DB 的驱动名称。 DriverName() string }
type JSONText ¶
type JSONText json.RawMessage
JSONText is a json.RawMessage, which is a []byte underneath. Value() validates the json format in the source, and returns an error if the json is not valid. Scan does no validation. JSONText additionally implements `Unmarshal`, which unmarshals the json within to an interface{}
func (JSONText) MarshalBinary ¶
func (JSONText) MarshalJSON ¶
MarshalJSON returns the *j as the JSON encoding of j.
func (*JSONText) UnmarshalBinary ¶
func (*JSONText) UnmarshalJSON ¶
UnmarshalJSON sets *j to a copy of data
type Mapper ¶
type Mapper struct { SqlBuilder // contains filtered or unexported fields }
type ModelWrapper ¶
type ModelWrapper struct {
// contains filtered or unexported fields
}
func NewModelWrapper ¶
func NewModelWrapper(dbList map[string]*DB, model interface{}) *ModelWrapper
func (*ModelWrapper) GetRelationDB ¶
func (m *ModelWrapper) GetRelationDB(connect string) *DB
func (*ModelWrapper) UnWrap ¶
func (m *ModelWrapper) UnWrap() interface{}
type ModelWrapperFactory ¶
type ModelWrapperFactory func(m interface{}) *ModelWrapper
type QueryStatus ¶
QueryStatus 表示查询执行后的状态。
type ReflectMapper ¶
type ReflectMapper struct {
// contains filtered or unexported fields
}
ReflectMapper 数据库字段与结构体属性的反射映射器。
func NewReflectMapper ¶
func NewReflectMapper(tagName string) *ReflectMapper
NewReflectMapper 输入<tagName>返回一个 反射映射器对象。
func (*ReflectMapper) FieldByName ¶
FieldByName 通过其映射名称返回字段,即 reflect.Value。
如果v的Kind不是Struct或v不可间接访问结构体,则会 panic。 如果找不到名称,则返回零值。
type SqlBuilder ¶
type SqlBuilder struct {
// contains filtered or unexported fields
}
SQL语句构造器
func (*SqlBuilder) Where ¶
func (s *SqlBuilder) Where(str string, args ...interface{})