Documentation ¶
Index ¶
- type GormDB
- func InitClickHouse(dataSource string, config GormDbConfig) (*GormDB, error)
- func InitMysql(dataSource string, config GormDbConfig) (*GormDB, error)
- func InitPostgreSql(dsn string, config GormDbConfig) (*GormDB, error)
- func InitSqlServer(dsn string, config GormDbConfig) (*GormDB, error)
- func InitSqlite3(dbFilePath string, config GormDbConfig) (*GormDB, error)
- func (s *GormDB) AutoMigrate(values ...interface{}) error
- func (s *GormDB) BeginTransaction() *GormDB
- func (s *GormDB) Commit() error
- func (s *GormDB) Create(value interface{}) (tx *gorm.DB)
- func (s *GormDB) Delete(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Exe1c(table string, dest interface{}, where string, args ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Exec(sql string, values ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Find(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) FindOne(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) First(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) FirstOrCreate(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Get(key string) (interface{}, bool)
- func (s *GormDB) Init(config *GormDbConfig) error
- func (s *GormDB) InsertSpecified(fields []string, exclude bool, value interface{}) (tx *gorm.DB)
- func (s *GormDB) Last(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Model(value interface{}) *gorm.DB
- func (s *GormDB) Raw(sql string, dest interface{}, values ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Rollback()
- func (s *GormDB) RollbackTo(name string)
- func (s *GormDB) Save(value interface{}) (tx *gorm.DB)
- func (s *GormDB) SavePoint(name string)
- func (s *GormDB) Select(value interface{}, conds ...interface{}) (tx *gorm.DB)
- func (s *GormDB) Session(config *gorm.Session) *gorm.DB
- func (s *GormDB) Set(key string, value interface{}) (tx *gorm.DB)
- func (s *GormDB) Update(model interface{}, column string, value interface{}) (tx *gorm.DB)
- func (s *GormDB) Updates(model interface{}, value interface{}) (tx *gorm.DB)
- func (s *GormDB) UpdatesByMap(model interface{}, value map[string]interface{}) (tx *gorm.DB)
- type GormDbConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GormDB ¶
func InitClickHouse ¶
func InitClickHouse(dataSource string, config GormDbConfig) (*GormDB, error)
初始化CLICKHOUSE
func InitMysql ¶
func InitMysql(dataSource string, config GormDbConfig) (*GormDB, error)
初始化MySQL OR TiDB(TiDB is compatible with MySQL protocol.)
func InitPostgreSql ¶ added in v1.2.23
func InitPostgreSql(dsn string, config GormDbConfig) (*GormDB, error)
初始化 dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
func InitSqlServer ¶ added in v1.2.23
func InitSqlServer(dsn string, config GormDbConfig) (*GormDB, error)
初始化 dsn := "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
func InitSqlite3 ¶
func InitSqlite3(dbFilePath string, config GormDbConfig) (*GormDB, error)
初始化sqlite3
Example: dbFilePath : /user/db/sqlite3.db
func (*GormDB) AutoMigrate ¶
package main
import (
"gorm.io/gorm" "gorm.io/driver/sqlite"
)
type Product struct { gorm.Model Code string Price uint } func main() { db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) if err != nil { panic("failed to connect database") } // 迁移 schema db.AutoMigrate(&Product{}) // Create db.Create(&Product{Code: "D42", Price: 100}) // Read var product Product db.First(&product, 1) // 根据整型主键查找 db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录 // Update - 将 product 的 price 更新为 200 db.Model(&product).Update("Price", 200) // Update - 更新多个字段 db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段 db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}) // Delete - 删除 product db.Delete(&product, 1) }
db.AutoMigrate(&User{})
db.AutoMigrate(&User{}, &Product{}, &Order{})
// Add table suffix when creating tables db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
func (*GormDB) BeginTransaction ¶
事物开始
func CreateAnimals(db *gorm.DB) error { // 再唠叨一下,事务一旦开始,你就应该使用 tx 处理数据 tx := db.Begin() defer func() { if r := recover(); r != nil { tx.Rollback() } }() if err := tx.Error; err != nil { return err } if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { tx.Rollback() return err } if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil { tx.Rollback() return err } return tx.Commit().Error }
func (*GormDB) Create ¶
Insert or Batch Insert
user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
users := []*User{ {Name: "Jinzhu", Age: 18, Birthday: time.Now()}, {Name: "Jackson", Age: 19, Birthday: time.Now()}, }
result := db.Create(&user) pass pointer of data to Create
user.ID returns inserted data's primary key
result.Error returns error
result.RowsAffected returns inserted records count
Create Hooks
func (u *User) BeforeCreate(tx *gorm.DB) (err error) { u.UUID = uuid.New() if u.Role == "admin" { return errors.New("invalid role") } return }
Default Values
type User struct { ID int64 Name string `gorm:"default:galeone"` Age int64 `gorm:"default:18"` }
func (*GormDB) Delete ¶
如果你的模型包含了 gorm.DeletedAt字段(该字段也被包含在gorm.Model中),那么该模型将会自动获得软删除的能力!
当调用Delete时,GORM并不会从数据库中删除该记录,而是将该记录的DeleteAt设置为当前时间,而后的一般查询方法将无法查找到此条记录。
func (*GormDB) Exec ¶
db.Exec("DROP TABLE users") db.Exec("UPDATE orders SET shipped_at = ? WHERE id IN ?", time.Now(), []int64{1, 2, 3})
// Exec with SQL Expression db.Exec("UPDATE users SET money = ? WHERE name = ?", gorm.Expr("money * ? + ?", 10000, 1), "jinzhu")
func (*GormDB) Find ¶
func (u *User) AfterFind(tx *gorm.DB) (err error) { // Custom logic after finding a user if u.Role == "" { u.Role = "user" // Set default role if not specified } return }
func (*GormDB) FindOne ¶
Get one record, no specified order SELECT * FROM users LIMIT 1;
check error ErrRecordNotFound
errors.Is(result.Error, gorm.ErrRecordNotFound)
func (*GormDB) First ¶
Get the first record ordered by primary key SELECT * FROM users ORDER BY id LIMIT 1;
var product Product db.First(&product, 1) // find product with integer primary key db.First(&product, "code = ?", "D42") // find product with code D42
func (*GormDB) FirstOrCreate ¶
func (*GormDB) Init ¶
func (s *GormDB) Init(config *GormDbConfig) error
func (*GormDB) InsertSpecified ¶
特殊字段处理 Insert or Batch Insert
func (*GormDB) Last ¶
Get last record, ordered by primary key desc
SELECT * FROM users ORDER BY id DESC LIMIT 1;
func (*GormDB) Raw ¶
type Result struct { ID int Name string Age int }
var result Result db.Raw("SELECT id, name, age FROM users WHERE id = ?", 3).Scan(&result)
db.Raw("SELECT id, name, age FROM users WHERE name = ?", "jinzhu").Scan(&result)
var age int db.Raw("SELECT SUM(age) FROM users WHERE role = ?", "admin").Scan(&age)
var users []User db.Raw("UPDATE users SET name = ? WHERE age = ? RETURNING id, name", "jinzhu", 20).Scan(&users)
func (*GormDB) RollbackTo ¶
事物回滚到name的点击
tx := db.Begin() tx.Create(&user1)
tx.SavePoint("sp1") tx.Create(&user2) tx.RollbackTo("sp1") // Rollback user2
tx.Commit() // Commit user1
func (*GormDB) Session ¶
更新用户并完全更新其所有关联
db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&user)
SQL:完全更新地址、用户、电子邮件表,包括现有的关联记录
func (*GormDB) Set ¶
clickhouse Set table options after AutoMigrate
db.Set("gorm:table_options", "ENGINE=Distributed(cluster, default, hits)").AutoMigrate(&User{})