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
- 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 Option
- type Options
- 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 ¶ added in v2.1.1
func JsonObject(value interface{}) (json.RawMessage, error)
func RegisterDialect ¶ added in v2.0.1
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 ¶ added in v2.1.0
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_lifetime" toml:"max_lifetime" json:"max_lifetime"` 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 ¶ added in v2.0.2
Beginx begins a transaction for default database and returns an *gosql.DB instead of an *sql.Tx.
func OpenWithDB ¶ added in v2.0.4
OpenWithDB open gosql.DB with sql.DB
func Relation ¶
func Relation(name string, fn BuilderChainFunc) *DB
Relation association table builder handle
func (*DB) Begin ¶ added in v2.0.2
Beginx begins a transaction and returns an *gosql.DB instead of an *sql.Tx.
func (*DB) DriverName ¶ added in v2.0.1
DriverName wrapper sqlx.DriverName
func (*DB) Model ¶ added in v2.0.1
Model database handler from to struct for example: gosql.Use("db2").Model(&users{})
func (*DB) Relation ¶ added in v2.0.1
func (w *DB) Relation(name string, fn BuilderChainFunc) *DB
Relation association table builder handle
func (*DB) Table ¶ added in v2.0.1
Table database handler from to table name for example: gosql.Use("db2").Table("users")
type Dialect ¶ added in v2.0.1
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 ¶ added in v2.0.1
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 ¶ added in v2.1.1
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 ¶ added in v2.1.1
func (JSONText) MarshalJSON ¶ added in v2.1.1
MarshalJSON returns the *j as the JSON encoding of j.
func (*JSONText) Unmarshal ¶ added in v2.1.1
Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.
func (*JSONText) UnmarshalBinary ¶ added in v2.1.1
func (*JSONText) UnmarshalJSON ¶ added in v2.1.1
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 ¶ added in v2.1.1
type ModelWrapper struct {
// contains filtered or unexported fields
}
func NewModelWrapper ¶ added in v2.1.1
func NewModelWrapper(dbList map[string]*DB, model interface{}) *ModelWrapper
func (*ModelWrapper) GetRelationDB ¶ added in v2.1.1
func (m *ModelWrapper) GetRelationDB(connect string) *DB
func (*ModelWrapper) UnWrap ¶ added in v2.1.1
func (m *ModelWrapper) UnWrap() interface{}
type ModelWrapperFactory ¶ added in v2.1.1
type ModelWrapperFactory func(m interface{}) *ModelWrapper
type Option ¶ added in v2.1.4
type Option func(*Options)
func WithMaxIdleConns ¶ added in v2.1.4
func WithMaxLifetimes ¶ added in v2.1.4
func WithMaxOpenConns ¶ added in v2.1.4
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{})