Documentation ¶
Index ¶
- Variables
- func Connect(configs map[string]*Config) (err error)
- func Exec(query string, args ...interface{}) (sql.Result, error)
- func Expr(expression string, args ...interface{}) *expr
- func Get(dest interface{}, query string, args ...interface{}) error
- func IsZero(val reflect.Value) bool
- func JsonObject(value interface{}) (json.RawMessage, error)
- func List() map[string]*sqlx.DB
- func NamedExec(query string, args interface{}) (sql.Result, error)
- func QueryRowx(query string, args ...interface{}) *sqlx.Row
- func Queryx(query 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(dest interface{}, query string, args ...interface{}) error
- func SetDefaultLink(db string)
- func SetLogger(l Logger)
- func SetLogging(logging bool)
- func Sqlx(name ...string) *sqlx.DB
- func Tx(fn func(tx *DB) error) error
- func Txx(ctx context.Context, fn func(ctx context.Context, tx *DB) error) error
- func ValueFilter(fields map[string]reflect.Value, zv []string) map[string]interface{}
- type Builder
- func (b *Builder) All() (err error)
- func (b *Builder) Count(zeroValues ...string) (num int64, err error)
- func (b *Builder) Create() (lastInsertId int64, err error)
- func (b *Builder) Delete(zeroValues ...string) (affected int64, err error)
- func (b *Builder) ForceIndex(i string) *Builder
- func (b *Builder) Get(zeroValues ...string) (err error)
- func (b *Builder) Hint(hint string) *Builder
- 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 BuilderChainFunc) *Builder
- func (b *Builder) Select(fields string) *Builder
- func (b *Builder) ShowSQL() *Builder
- func (b *Builder) Update(zeroValues ...string) (affected int64, err error)
- func (b *Builder) Where(str string, args ...interface{}) *Builder
- type BuilderChainFunc
- type Config
- type DB
- func (w *DB) Begin() (*DB, error)
- func (w *DB) Commit() error
- func (w *DB) DriverName() string
- func (w *DB) Exec(query string, args ...interface{}) (result sql.Result, err error)
- func (w *DB) Get(dest interface{}, query string, args ...interface{}) (err error)
- func (w *DB) Model(m interface{}) *Builder
- func (w *DB) NamedExec(query string, args interface{}) (result sql.Result, err error)
- func (w *DB) Preparex(query string) (*sqlx.Stmt, error)
- func (w *DB) QueryRowx(query string, args ...interface{}) (rows *sqlx.Row)
- func (w *DB) Queryx(query string, args ...interface{}) (rows *sqlx.Rows, err error)
- func (w *DB) Rebind(query string) string
- func (w *DB) Relation(name string, fn BuilderChainFunc) *DB
- func (w *DB) Rollback() error
- func (w *DB) Select(dest interface{}, query string, args ...interface{}) (err error)
- func (w *DB) ShowSql() *DB
- func (w *DB) Table(t string) *Mapper
- func (w *DB) Tx(fn func(w *DB) error) (err error)
- func (w *DB) Txx(ctx context.Context, fn func(ctx context.Context, tx *DB) error) (err error)
- func (w *DB) WithContext(ctx context.Context) *Builder
- type Dialect
- type Hook
- type IModel
- type ISqlx
- 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 Logger
- type Mapper
- func (m *Mapper) Count() (num int64, err error)
- func (m *Mapper) Create(data map[string]interface{}) (lastInsertId int64, err error)
- func (m *Mapper) Delete() (affected 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 ( // Insert database automatically updates fields AUTO_CREATE_TIME_FIELDS = []string{ "create_time", "create_at", "created_at", "update_time", "update_at", "updated_at", } // Update database automatically updates fields AUTO_UPDATE_TIME_FIELDS = []string{ "update_time", "update_at", "updated_at", } )
var FatalExit = true
If database fatal exit
Functions ¶
func Expr ¶
func Expr(expression string, args ...interface{}) *expr
Expr generate raw SQL expression, for example:
gosql.Table("user").Update(map[string]interface{}{"price", gorm.Expr("price * ? + ?", 2, 100)})
func JsonObject ¶
func JsonObject(value interface{}) (json.RawMessage, error)
func RegisterDialect ¶
RegisterDialect register new dialect
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 }
func WithContext ¶
Model construct SQL from Struct with context
func (*Builder) Relation ¶
func (b *Builder) Relation(fieldName string, fn BuilderChainFunc) *Builder
Relation association table builder handle
type BuilderChainFunc ¶
type BuilderChainFunc func(b *Builder)
type Config ¶
type Config struct { Enable bool `yml:"enable" toml:"enable" json:"enable"` Driver string `yml:"driver" toml:"driver" json:"driver"` Dsn string `yml:"dsn" toml:"dsn" json:"dsn"` MaxOpenConns int `yml:"max_open_conns" toml:"max_open_conns" json:"max_open_conns"` MaxIdleConns int `yml:"max_idle_conns" toml:"max_idle_conns" json:"max_idle_conns"` MaxLifetime int `yml:"max_lefttime" toml:"max_lefttime" json:"max_lefttime"` ShowSql bool `yml:"show_sql" toml:"show_sql" json:"show_sql"` }
Config is database connection configuration
type DB ¶
type DB struct { RelationMap map[string]BuilderChainFunc // contains filtered or unexported fields }
func Begin ¶
Beginx begins a transaction for default database and returns an *gosql.DB instead of an *sql.Tx.
func OpenWithDB ¶
OpenWithDB open gosql.DB with sql.DB
func Relation ¶
func Relation(name string, fn BuilderChainFunc) *DB
Relation association table builder handle
func (*DB) Model ¶
Model database handler from to struct for example: gosql.Use("db2").Model(&users{})
func (*DB) Relation ¶
func (w *DB) Relation(name string, fn BuilderChainFunc) *DB
Relation association table builder handle
func (*DB) Table ¶
Table database handler from to table name for example: gosql.Use("db2").Table("users")
type Dialect ¶
type Dialect interface { // GetName get dialect's name GetName() string // Quote quotes field name to avoid SQL parsing exceptions by using a reserved word as a field name Quote(key string) string // Placeholder is where value holder default "?" Placeholder() string }
Dialect interface contains behaviors that differ across SQL database
func GetDialect ¶
GetDialect gets the dialect for the specified dialect name
type ISqlx ¶
type ISqlx interface { Queryx(query string, args ...interface{}) (*sqlx.Rows, error) QueryRowx(query string, args ...interface{}) *sqlx.Row Get(dest interface{}, query string, args ...interface{}) error Select(dest interface{}, query string, args ...interface{}) error Exec(query string, args ...interface{}) (sql.Result, error) NamedExec(query string, arg interface{}) (sql.Result, error) Preparex(query string) (*sqlx.Stmt, error) Rebind(query string) string 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 Logger ¶
type Logger interface {
Printf(format string, v ...interface{})
}
Logger represents a logging collector. You can pass a logging collector to gosql.SetLogger(myCollector) to make it collect QueryStatus messages after executing a query.
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 represents the status of a query after being executed.
func (*QueryStatus) String ¶
func (q *QueryStatus) String() string
String returns a formatted log message.
type ReflectMapper ¶
type ReflectMapper struct {
// contains filtered or unexported fields
}
func NewReflectMapper ¶
func NewReflectMapper(tagName string) *ReflectMapper
func (*ReflectMapper) 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.
type SQLBuilder ¶
type SQLBuilder struct {
// contains filtered or unexported fields
}
func (*SQLBuilder) Where ¶
func (s *SQLBuilder) Where(str string, args ...interface{})