Documentation ¶
Index ¶
- type DB
- func (db *DB) AddError(err error) error
- func (db *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) Interface
- func (db *DB) AddIndex(indexName string, columns ...string) Interface
- func (db *DB) AddUniqueIndex(indexName string, columns ...string) Interface
- func (db *DB) Assign(attrs ...interface{}) Interface
- func (db *DB) Association(column string) *gorm.Association
- func (db *DB) Attrs(attrs ...interface{}) Interface
- func (db *DB) AutoMigrate(values ...interface{}) Interface
- func (db *DB) Begin() Interface
- func (db *DB) BlockGlobalUpdate(enable bool) Interface
- func (db *DB) Callback() *gorm.Callback
- func (db *DB) Close() error
- func (db *DB) Commit() Interface
- func (db *DB) CommonDB() gorm.SQLCommon
- func (db *DB) Count(value interface{}) Interface
- func (db *DB) Create(value interface{}) Interface
- func (db *DB) CreateTable(models ...interface{}) Interface
- func (db *DB) DB() *sql.DB
- func (db *DB) Debug() Interface
- func (db *DB) Delete(value interface{}, where ...interface{}) Interface
- func (db *DB) Dialect() gorm.Dialect
- func (db *DB) DropColumn(column string) Interface
- func (db *DB) DropTable(values ...interface{}) Interface
- func (db *DB) DropTableIfExists(values ...interface{}) Interface
- func (db *DB) Error() error
- func (db *DB) Exec(sql string, values ...interface{}) Interface
- func (db *DB) Find(out interface{}, where ...interface{}) Interface
- func (db *DB) First(out interface{}, where ...interface{}) Interface
- func (db *DB) FirstOrCreate(out interface{}, where ...interface{}) Interface
- func (db *DB) FirstOrInit(out interface{}, where ...interface{}) Interface
- func (db *DB) Get(name string) (value interface{}, ok bool)
- func (db *DB) GetErrors() []error
- func (db *DB) GetGormDB() *gorm.DB
- func (db *DB) Group(query string) Interface
- func (db *DB) HasBlockGlobalUpdate() bool
- func (db *DB) HasTable(value interface{}) bool
- func (db *DB) Having(query interface{}, values ...interface{}) Interface
- func (db *DB) InstantSet(name string, value interface{}) Interface
- func (db *DB) Joins(query string, args ...interface{}) Interface
- func (db *DB) Last(out interface{}, where ...interface{}) Interface
- func (db *DB) Limit(limit interface{}) Interface
- func (db *DB) LogMode(enable bool) Interface
- func (db *DB) Model(value interface{}) Interface
- func (db *DB) ModifyColumn(column string, typ string) Interface
- func (db *DB) New() Interface
- func (db *DB) NewRecord(value interface{}) bool
- func (db *DB) Not(query interface{}, args ...interface{}) Interface
- func (db *DB) Offset(offset interface{}) Interface
- func (db *DB) Omit(columns ...string) Interface
- func (db *DB) Or(query interface{}, args ...interface{}) Interface
- func (db *DB) Order(value interface{}, reorder ...bool) Interface
- func (db *DB) Pluck(column string, value interface{}) Interface
- func (db *DB) Preload(column string, conditions ...interface{}) Interface
- func (db *DB) Raw(sql string, values ...interface{}) Interface
- func (db *DB) RecordNotFound() bool
- func (db *DB) Related(value interface{}, foreignKeys ...string) Interface
- func (db *DB) RemoveForeignKey(field string, dest string) Interface
- func (db *DB) RemoveIndex(indexName string) Interface
- func (db *DB) Rollback() Interface
- func (db *DB) Row() Row
- func (db *DB) Rows() (Rows, error)
- func (db *DB) RowsAffected() int64
- func (db *DB) Save(value interface{}) Interface
- func (db *DB) Scan(dest interface{}) Interface
- func (db *DB) ScanRows(rows *sql.Rows, result interface{}) error
- func (db *DB) Scopes(funcs ...func(*gorm.DB) *gorm.DB) Interface
- func (db *DB) Select(query interface{}, args ...interface{}) Interface
- func (db *DB) Set(name string, value interface{}) Interface
- func (db *DB) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)
- func (db *DB) SetLogger(log mysql.Logger)
- func (db *DB) SingularTable(enable bool)
- func (db *DB) Table(name string) Interface
- func (db *DB) Take(out interface{}, where ...interface{}) Interface
- func (db *DB) Unscoped() Interface
- func (db *DB) Update(attrs ...interface{}) Interface
- func (db *DB) UpdateColumn(attrs ...interface{}) Interface
- func (db *DB) UpdateColumns(values interface{}) Interface
- func (db *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) Interface
- func (db *DB) Value() interface{}
- func (db *DB) Where(query interface{}, args ...interface{}) Interface
- func (db *DB) WithContext(ctx context.Context) Interface
- type Interface
- type Row
- type Rows
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a wrapper struct around a *gorm.DB
func (*DB) AddForeignKey ¶
AddForeignKey Add foreign key to the given scope, e.g:
db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
func (*DB) AddUniqueIndex ¶
AddUniqueIndex add unique index for columns with given name
func (*DB) Assign ¶
Assign assign result with argument regardless it is found or not with `FirstOrInit` https://jinzhu.github.io/gorm/crud.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (*DB) Association ¶
func (db *DB) Association(column string) *gorm.Association
Association start `Association Mode` to handler relations things easir in that mode, refer: https://jinzhu.github.io/gorm/associations.html#association-mode
func (*DB) Attrs ¶
Attrs initialize struct with argument if record not found with `FirstOrInit` https://jinzhu.github.io/gorm/crud.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (*DB) AutoMigrate ¶
AutoMigrate run auto migration for given models, will only add missing fields, won't delete/change current data
func (*DB) BlockGlobalUpdate ¶
BlockGlobalUpdate if true, generates an error on update/delete without where clause. This is to prevent eventual error with empty objects updates/deletions
func (*DB) Callback ¶
Callback return `Callbacks` container, you could add/change/delete callbacks with it
db.Callback().Create().Register("update_created_at", updateCreated)
Refer https://jinzhu.github.io/gorm/development.html#callbacks
func (*DB) Close ¶
Close close current db connection. If database connection is not an io.Closer, returns an error.
func (*DB) CommonDB ¶
CommonDB return the underlying `*sql.DB` or `*sql.Tx` instance, mainly intended to allow coexistence with legacy non-GORM code.
func (*DB) CreateTable ¶
CreateTable create table for models
func (*DB) DB ¶
DB get `*sql.DB` from current connection If the underlying database connection is not a *sql.DB, returns nil
func (*DB) Delete ¶
Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
func (*DB) DropTableIfExists ¶
DropTableIfExists drop table if it is exist
func (*DB) FirstOrCreate ¶
FirstOrCreate find first matched record or create a new one with given conditions (only works with struct, map conditions) https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (*DB) FirstOrInit ¶
FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions) https://jinzhu.github.io/gorm/crud.html#firstorinit
func (*DB) HasBlockGlobalUpdate ¶
HasBlockGlobalUpdate return state of block
func (*DB) InstantSet ¶
InstantSet instant set setting, will affect current db
func (*DB) Joins ¶
Joins specify Joins conditions
db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
func (*DB) LogMode ¶
LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs
func (*DB) Model ¶
Model specify the model you would like to run db operations
// update all users's name to `hello` db.Model(&User{}).Update("name", "hello") // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello` db.Model(&user).Update("name", "hello")
func (*DB) ModifyColumn ¶
ModifyColumn modify column to type
func (*DB) Offset ¶
Offset specify the number of records to skip before starting to return the records
func (*DB) Omit ¶
Omit specify fields that you want to ignore when saving to database for creating, updating
func (*DB) Order ¶
Order specify order when retrieve records from database, set reorder to `true` to overwrite defined conditions
db.Order("name DESC") db.Order("name DESC", true) // reorder db.Order(gorm.Expr("name = ? DESC", "first")) // sql expression
func (*DB) Pluck ¶
Pluck used to query single column from a model as a map
var ages []int64 db.Find(&users).Pluck("age", &ages)
func (*DB) Preload ¶
Preload preload associations with given conditions
db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
func (*DB) Raw ¶
Raw use raw sql as conditions, won't run it unless invoked by other methods
db.Raw("SELECT name, age FROM users WHERE name = ?", 3).Scan(&result)
func (*DB) RecordNotFound ¶
RecordNotFound check if returning ErrRecordNotFound error
func (*DB) RemoveForeignKey ¶
RemoveForeignKey Remove foreign key from the given scope, e.g:
db.Model(&User{}).RemoveForeignKey("city_id", "cities(id)")
func (*DB) RemoveIndex ¶
RemoveIndex remove index with name
func (*DB) RowsAffected ¶
RowsAffected is a wrapper function for the RowsAffected field
func (*DB) Save ¶
Save update value in database, if the value doesn't have primary key, will insert it
func (*DB) Scopes ¶
Scopes pass current database connection to arguments `func(*DB) *DB`, which could be used to add conditions dynamically
func AmountGreaterThan1000(db *gorm.DB) *gorm.DB { return db.Where("amount > ?", 1000) } func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB { return func (db *gorm.DB) *gorm.DB { return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status) } } db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders)
func (*DB) Select ¶
Select specify fields that you want to retrieve from database when querying, by default, will select all fields; When creating/updating, specify fields that you want to save to database
func (*DB) Set ¶
Set set setting by name, which could be used in callbacks, will clone a new db, and update its setting
func (*DB) SetJoinTableHandler ¶
func (db *DB) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)
SetJoinTableHandler set a model's join table handler for a relation
func (*DB) SingularTable ¶
SingularTable use singular table by default
func (*DB) Take ¶
Take return a record that match given conditions, the order will depend on the database implementation
func (*DB) Unscoped ¶
Unscoped return all record including deleted record, refer Soft Delete https://jinzhu.github.io/gorm/crud.html#soft-delete
func (*DB) Update ¶
Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (*DB) UpdateColumn ¶
UpdateColumn update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (*DB) UpdateColumns ¶
UpdateColumns update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (*DB) Updates ¶
Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (*DB) Value ¶
func (db *DB) Value() interface{}
Value is a wrapper function for the Value field
func (*DB) Where ¶
Where return a new relation, filter records with given conditions, accepts `map`, `struct` or `string` as conditions, refer http://jinzhu.github.io/gorm/crud.html#query
type Interface ¶
type Interface interface { // Adds context propagation WithContext(ctx context.Context) Interface // Allow field values to be accessed via function calls Value() interface{} Error() error RowsAffected() int64 // Allow users access to the underlying gorm DB GetGormDB() *gorm.DB // Wrap the functions New() Interface Close() error DB() *sql.DB CommonDB() gorm.SQLCommon Dialect() gorm.Dialect Callback() *gorm.Callback SetLogger(log mysql.Logger) LogMode(enable bool) Interface BlockGlobalUpdate(enable bool) Interface HasBlockGlobalUpdate() bool SingularTable(enable bool) Where(query interface{}, args ...interface{}) Interface Or(query interface{}, args ...interface{}) Interface Not(query interface{}, args ...interface{}) Interface Limit(limit interface{}) Interface Offset(offset interface{}) Interface Order(value interface{}, reorder ...bool) Interface Select(query interface{}, args ...interface{}) Interface Omit(columns ...string) Interface Group(query string) Interface Having(query interface{}, values ...interface{}) Interface Joins(query string, args ...interface{}) Interface Scopes(funcs ...func(*gorm.DB) *gorm.DB) Interface Unscoped() Interface Assign(attrs ...interface{}) Interface Attrs(attrs ...interface{}) Interface First(out interface{}, where ...interface{}) Interface Take(out interface{}, where ...interface{}) Interface Last(out interface{}, where ...interface{}) Interface Find(out interface{}, where ...interface{}) Interface Scan(dest interface{}) Interface Row() Row Rows() (Rows, error) ScanRows(rows *sql.Rows, result interface{}) error Pluck(column string, value interface{}) Interface Count(value interface{}) Interface Related(value interface{}, foreignKeys ...string) Interface FirstOrInit(out interface{}, where ...interface{}) Interface FirstOrCreate(out interface{}, where ...interface{}) Interface Update(attrs ...interface{}) Interface Updates(values interface{}, ignoreProtectedAttrs ...bool) Interface UpdateColumn(attrs ...interface{}) Interface UpdateColumns(values interface{}) Interface Save(value interface{}) Interface Create(value interface{}) Interface Delete(value interface{}, where ...interface{}) Interface Raw(sql string, values ...interface{}) Interface Exec(sql string, values ...interface{}) Interface Model(value interface{}) Interface Table(name string) Interface Debug() Interface Begin() Interface Commit() Interface Rollback() Interface NewRecord(value interface{}) bool RecordNotFound() bool CreateTable(models ...interface{}) Interface DropTable(values ...interface{}) Interface DropTableIfExists(values ...interface{}) Interface HasTable(value interface{}) bool AutoMigrate(values ...interface{}) Interface ModifyColumn(column string, typ string) Interface DropColumn(column string) Interface AddIndex(indexName string, columns ...string) Interface AddUniqueIndex(indexName string, columns ...string) Interface RemoveIndex(indexName string) Interface AddForeignKey(field string, dest string, onDelete string, onUpdate string) Interface RemoveForeignKey(field string, dest string) Interface Association(column string) *gorm.Association Preload(column string, conditions ...interface{}) Interface Set(name string, value interface{}) Interface InstantSet(name string, value interface{}) Interface Get(name string) (value interface{}, ok bool) SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface) AddError(err error) error GetErrors() []error }
Interface contains all of the funcs a *gorm.DB has