// Code generated by goctl. DO NOT EDIT. // versions: // goctl version: 1.8.4 package model import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( photoFieldNames = builder.RawFieldNames(&Photo{}) photoRows = strings.Join(photoFieldNames, ",") photoRowsExpectAutoSet = strings.Join(stringx.Remove(photoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") photoRowsWithPlaceHolder = strings.Join(stringx.Remove(photoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" ) type ( photoModel interface { Insert(ctx context.Context, data *Photo) (sql.Result, error) FindOne(ctx context.Context, id int64) (*Photo, error) Update(ctx context.Context, data *Photo) error Delete(ctx context.Context, id int64) error } defaultPhotoModel struct { conn sqlx.SqlConn table string } Photo struct { Id int64 `db:"id"` Title string `db:"title"` // 照片标题 Description sql.NullString `db:"description"` // 照片描述 FilePath string `db:"file_path"` // 原图路径 ThumbnailPath string `db:"thumbnail_path"` // 缩略图路径 UserId int64 `db:"user_id"` // 用户ID CategoryId int64 `db:"category_id"` // 分类ID CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` } ) func newPhotoModel(conn sqlx.SqlConn) *defaultPhotoModel { return &defaultPhotoModel{ conn: conn, table: "`photo`", } } func (m *defaultPhotoModel) Delete(ctx context.Context, id int64) error { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, query, id) return err } func (m *defaultPhotoModel) FindOne(ctx context.Context, id int64) (*Photo, error) { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", photoRows, m.table) var resp Photo err := m.conn.QueryRowCtx(ctx, &resp, query, id) switch err { case nil: return &resp, nil case sqlx.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultPhotoModel) Insert(ctx context.Context, data *Photo) (sql.Result, error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, photoRowsExpectAutoSet) ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Description, data.FilePath, data.ThumbnailPath, data.UserId, data.CategoryId) return ret, err } func (m *defaultPhotoModel) Update(ctx context.Context, data *Photo) error { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, photoRowsWithPlaceHolder) _, err := m.conn.ExecCtx(ctx, query, data.Title, data.Description, data.FilePath, data.ThumbnailPath, data.UserId, data.CategoryId, data.Id) return err } func (m *defaultPhotoModel) tableName() string { return m.table }