Documentation ¶
Index ¶
- func BuildSQLUpdate(update any) (map[string]any, error)
- func BuildSQLWhereExpr(where any) (clause.Expression, error)
- func JSONContains(column string, data any) clause.Expression
- func JSONContainsExprs(column string, data any) ([]clause.Expression, error)
- func JSONSetExpr(column string, data any) clause.Expr
- type SQLUpdater
- type SQLWhereExprBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildSQLUpdate ¶
BuildSQLUpdate build Update struct into sql update map
💡 HINT:
⚠️ WARNING: fields of update must be pointers
🚀 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. 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 BuildSQLWhereExpr ¶ added in v0.1.8
func BuildSQLWhereExpr(where any) (clause.Expression, error)
BuildSQLWhereExpr build Where model struct into query & args in SQL
💡 HINT:
⚠️ WARNING:
🚀 example:
// model table 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 to be updated 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, } gwhere, err := BuildSQLWhereExpr(attrs) if err != nil { // handle error } // SQL: update table_abc set name="byte-er" where id = 1 if err := db.Find(&pos).Where(gwhere).Error; err != nil { logs.Error("fins table abc failed: %s", err) } }
func JSONContains ¶ added in v0.1.8
func JSONContains(column string, data any) clause.Expression
func JSONContainsExprs ¶ added in v0.1.8
func JSONContainsExprs(column string, data any) ([]clause.Expression, error)
Types ¶
type SQLUpdater ¶
SQLUpdater update SQL generator
type SQLWhereExprBuilder ¶ added in v0.1.8
type SQLWhereExprBuilder func(column string, data any) (clause.Expression, error)
SQLWhereExprBuilder where SQL generator
func GetWhereExpr ¶ added in v0.1.8
func GetWhereExpr(operator string) (SQLWhereExprBuilder, error)
Click to show internal directories.
Click to hide internal directories.