使用ddl生成结构, 支持mysql连接使用
This commit is contained in:
24
model/fsfaqmodel.go
Executable file
24
model/fsfaqmodel.go
Executable file
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ FsFaqModel = (*customFsFaqModel)(nil)
|
||||
|
||||
type (
|
||||
// FsFaqModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customFsFaqModel.
|
||||
FsFaqModel interface {
|
||||
fsFaqModel
|
||||
}
|
||||
|
||||
customFsFaqModel struct {
|
||||
*defaultFsFaqModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewFsFaqModel returns a model for the database table.
|
||||
func NewFsFaqModel(conn sqlx.SqlConn) FsFaqModel {
|
||||
return &customFsFaqModel{
|
||||
defaultFsFaqModel: newFsFaqModel(conn),
|
||||
}
|
||||
}
|
||||
90
model/fsfaqmodel_gen.go
Executable file
90
model/fsfaqmodel_gen.go
Executable file
@@ -0,0 +1,90 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
fsFaqFieldNames = builder.RawFieldNames(&FsFaq{})
|
||||
fsFaqRows = strings.Join(fsFaqFieldNames, ",")
|
||||
fsFaqRowsExpectAutoSet = strings.Join(stringx.Remove(fsFaqFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
fsFaqRowsWithPlaceHolder = strings.Join(stringx.Remove(fsFaqFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
fsFaqModel interface {
|
||||
Insert(ctx context.Context, data *FsFaq) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*FsFaq, error)
|
||||
Update(ctx context.Context, data *FsFaq) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultFsFaqModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
FsFaq struct {
|
||||
Id int64 `db:"id"`
|
||||
TagId int64 `db:"tag_id"` // 分类ID
|
||||
TagName string `db:"tag_name"` // 分类名称
|
||||
Title string `db:"title"` // 标题
|
||||
Content sql.NullString `db:"content"` // 内容
|
||||
Status int64 `db:"status"` // 状态(0:禁用,1:启用)
|
||||
Sort int64 `db:"sort"` // 排序
|
||||
Ctime sql.NullInt64 `db:"ctime"` // 添加时间
|
||||
}
|
||||
)
|
||||
|
||||
func newFsFaqModel(conn sqlx.SqlConn) *defaultFsFaqModel {
|
||||
return &defaultFsFaqModel{
|
||||
conn: conn,
|
||||
table: "`fs_faq`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultFsFaqModel) 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 *defaultFsFaqModel) FindOne(ctx context.Context, id int64) (*FsFaq, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsFaqRows, m.table)
|
||||
var resp FsFaq
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultFsFaqModel) Insert(ctx context.Context, data *FsFaq) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, fsFaqRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.TagId, data.TagName, data.Title, data.Content, data.Status, data.Sort, data.Ctime)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultFsFaqModel) Update(ctx context.Context, data *FsFaq) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsFaqRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.TagId, data.TagName, data.Title, data.Content, data.Status, data.Sort, data.Ctime, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultFsFaqModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
24
model/fsfontmodel.go
Executable file
24
model/fsfontmodel.go
Executable file
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ FsFontModel = (*customFsFontModel)(nil)
|
||||
|
||||
type (
|
||||
// FsFontModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customFsFontModel.
|
||||
FsFontModel interface {
|
||||
fsFontModel
|
||||
}
|
||||
|
||||
customFsFontModel struct {
|
||||
*defaultFsFontModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewFsFontModel returns a model for the database table.
|
||||
func NewFsFontModel(conn sqlx.SqlConn) FsFontModel {
|
||||
return &customFsFontModel{
|
||||
defaultFsFontModel: newFsFontModel(conn),
|
||||
}
|
||||
}
|
||||
87
model/fsfontmodel_gen.go
Executable file
87
model/fsfontmodel_gen.go
Executable file
@@ -0,0 +1,87 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
fsFontFieldNames = builder.RawFieldNames(&FsFont{})
|
||||
fsFontRows = strings.Join(fsFontFieldNames, ",")
|
||||
fsFontRowsExpectAutoSet = strings.Join(stringx.Remove(fsFontFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
fsFontRowsWithPlaceHolder = strings.Join(stringx.Remove(fsFontFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
fsFontModel interface {
|
||||
Insert(ctx context.Context, data *FsFont) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*FsFont, error)
|
||||
Update(ctx context.Context, data *FsFont) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultFsFontModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
FsFont struct {
|
||||
Id int64 `db:"id"` // id
|
||||
Title string `db:"title"` // 字体名字
|
||||
LinuxFontname string `db:"linux_fontname"` // linux对应字体名
|
||||
FilePath string `db:"file_path"` // 字体文件路径
|
||||
Sort int64 `db:"sort"` // 排序
|
||||
}
|
||||
)
|
||||
|
||||
func newFsFontModel(conn sqlx.SqlConn) *defaultFsFontModel {
|
||||
return &defaultFsFontModel{
|
||||
conn: conn,
|
||||
table: "`fs_font`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultFsFontModel) 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 *defaultFsFontModel) FindOne(ctx context.Context, id int64) (*FsFont, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsFontRows, m.table)
|
||||
var resp FsFont
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultFsFontModel) Insert(ctx context.Context, data *FsFont) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, fsFontRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.LinuxFontname, data.FilePath, data.Sort)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultFsFontModel) Update(ctx context.Context, data *FsFont) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsFontRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.LinuxFontname, data.FilePath, data.Sort, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultFsFontModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
model/vars.go
Normal file
5
model/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
Reference in New Issue
Block a user