Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildSQLUpdate ¶
BuildSQLUpdate
@Description: build Update struct into sql update map
@param update: Update struct
@return m: map of sql update
@return err:
@example:
// model of table_abc type TableAbc struct { ID int64 `gorm:"column:id"` Name string `gorm:"column:name"` Age int `gorm:"column:p_age"` } func (TableAbc) TableName() string { return "table_abc" } // fields need to update. ⚠️ WARNING: must be pointers type TableAbcUpdate struct { Name *string `sql_field:"name"` Age *int `sql_field:"p_age"` } var name = "byte-er" attrs = TableAbcUpdate{ Name: &name } // SQL: update table_abc set name="byte-er" where id = 1 attrs, err := BuildSQLUpdate(attrs) if err != nil{ // do something } if err := db.Model(TableAbc{}).Where("id = ?", id).Updates(attrs).Error; err != nil { logs.Error("update table abc failed: %s", err) }
func BuildSQLWhere ¶
BuildSQLWhere
@Description: 将 Where model struct 编译为 sql 语句中 query 和 args
@param where: Where model
@return query:
@return args:
@return err:
@example
// model 表 type TableAbc struct { ID int64 `gorm:"column:id"` Name string `gorm:"column:name"` Age int `gorm:"column:p_age"` } func (TableAbc) TableName() string { return "table_abc" } // 需要更新的字段 type TableAbcWhere struct { Name *string `sql_field:"name" sql_operator:"like"` Age *int `sql_field:"p_age"` } func example() { var name = "name%" var age = 20 attrs := TableAbcWhere{ Name: &name, Age: &age, } query, args, err := BuildSQLWhere(attrs) if err != nil { // handle error } // 下面即 sql: update table_abc set name="byte-er" where id = 1 if err := db.Find(&pos).Where(query, args...).Error; err != nil { logs.Error("fins table abc failed: %s", err) } }
Types ¶
type OperatorFilter ¶
Click to show internal directories.
Click to hide internal directories.