代码拉取完成,页面将自动刷新
package mysql
import (
"context"
"gitee.com/yzsunjianguo/sponge/pkg/mysql/query"
"gorm.io/gorm"
)
// TableName get table name
func TableName(table interface{}) string {
return GetTableName(table)
}
// Create a new record
// the param of 'table' must be pointer, eg: &StructName
func Create(ctx context.Context, db *gorm.DB, table interface{}) error {
return db.WithContext(ctx).Create(table).Error
}
// Delete record
// the param of 'table' must be pointer, eg: &StructName
func Delete(ctx context.Context, db *gorm.DB, table interface{}, queryCondition interface{}, args ...interface{}) error {
return db.WithContext(ctx).Where(queryCondition, args...).Delete(table).Error
}
// DeleteByID delete record by id
// the param of 'table' must be pointer, eg: &StructName
func DeleteByID(ctx context.Context, db *gorm.DB, table interface{}, id interface{}) error {
return db.WithContext(ctx).Where("id = ?", id).Delete(table).Error
}
// Update record
// the param of 'table' must be pointer, eg: &StructName
func Update(ctx context.Context, db *gorm.DB, table interface{}, column string, value interface{}, queryCondition interface{}, args ...interface{}) error {
return db.WithContext(ctx).Model(table).Where(queryCondition, args...).Update(column, value).Error
}
// Updates record
// the param of 'table' must be pointer, eg: &StructName
func Updates(ctx context.Context, db *gorm.DB, table interface{}, update KV, queryCondition interface{}, args ...interface{}) error {
return db.WithContext(ctx).Model(table).Where(queryCondition, args...).Updates(update).Error
}
// Get one record
// the param of 'table' must be pointer, eg: &StructName
func Get(ctx context.Context, db *gorm.DB, table interface{}, queryCondition interface{}, args ...interface{}) error {
return db.WithContext(ctx).Where(queryCondition, args...).First(table).Error
}
// GetByID get record by id
func GetByID(ctx context.Context, db *gorm.DB, table interface{}, id interface{}) error {
return db.WithContext(ctx).Where("id = ?", id).First(table).Error
}
// List multiple records, starting from page 0
// the param of 'tables' must be a slice, eg: []StructName
func List(ctx context.Context, db *gorm.DB, tables interface{}, page *query.Page, queryCondition interface{}, args ...interface{}) error {
return db.WithContext(ctx).Order(page.Sort()).Limit(page.Size()).Offset(page.Offset()).Where(queryCondition, args...).Find(tables).Error
}
// Count number of records
// the param of 'table' must be pointer, eg: &StructName
func Count(ctx context.Context, db *gorm.DB, table interface{}, queryCondition interface{}, args ...interface{}) (int64, error) {
var count int64
err := db.WithContext(ctx).Model(table).Where(queryCondition, args...).Count(&count).Error
return count, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。