You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB
Go

3 years ago
// Code generated by goctl. DO NOT EDIT!
package model
import (
"context"
3 years ago
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
3 years ago
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stringx"
3 years ago
"gorm.io/gorm"
3 years ago
)
var (
userFieldNames = builder.RawFieldNames(&User{})
userRows = strings.Join(userFieldNames, ",")
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`create_time`", "`update_time`"), ",")
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)
type (
userModel interface {
3 years ago
Insert(ctx context.Context, data *User) error
3 years ago
FindOne(ctx context.Context, id int64) (*User, error)
Update(ctx context.Context, data *User) error
Delete(ctx context.Context, id int64) error
}
defaultUserModel struct {
3 years ago
conn *gorm.DB
3 years ago
table string
}
User struct {
Id int64 `gorm:"primaryKey"` // 用户ID
Username string `gorm:"column:username"` // 用户名
CreateTime time.Time `gorm:"column:create_time;default:null"` // 创建时间
UpdateTime time.Time `gorm:"column:update_time;default:null"` // 更新时间
3 years ago
}
)
3 years ago
func newUserModel(conn *gorm.DB) *defaultUserModel {
3 years ago
return &defaultUserModel{
conn: conn,
table: "`user`",
}
}
3 years ago
func (m *defaultUserModel) Insert(ctx context.Context, data *User) error {
err := m.conn.WithContext(ctx).Create(data).Error
3 years ago
return err
3 years ago
}
func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
var resp User
3 years ago
err := m.conn.WithContext(ctx).Model(&User{}).Where("`id` = ?", id).Take(&resp).Error
3 years ago
switch err {
case nil:
return &resp, nil
3 years ago
case gormc.ErrNotFound:
3 years ago
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
3 years ago
err := m.conn.WithContext(ctx).Save(data).Error
3 years ago
return err
}
func (m *defaultUserModel) Delete(ctx context.Context, id int64) error {
3 years ago
err := m.conn.WithContext(ctx).Delete(&User{}, id).Error
3 years ago
return err
}
func (m *defaultUserModel) tableName() string {
return m.table
}
3 years ago
func (User) TableName() string {
model := newUserModel(nil)
3 years ago
return model.tableName()
}