This commit is contained in:
12
backend_v1/database/.transform.yaml
Normal file
12
backend_v1/database/.transform.yaml
Normal file
@@ -0,0 +1,12 @@
|
||||
ignores:
|
||||
- migrations
|
||||
- river_client
|
||||
- river_client_queue
|
||||
- river_job
|
||||
- river_leader
|
||||
- river_migration
|
||||
- river_queue
|
||||
imports:
|
||||
- go.ipao.vip/gen
|
||||
field_type:
|
||||
field_relate:
|
||||
55
backend_v1/database/database.go
Normal file
55
backend_v1/database/database.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
//go:embed migrations/*
|
||||
var MigrationFS embed.FS
|
||||
|
||||
func Truncate(ctx context.Context, db *sql.DB, tableName ...string) error {
|
||||
for _, name := range tableName {
|
||||
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
|
||||
if _, err := db.ExecContext(ctx, sql); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func WrapLike(v string) string {
|
||||
return "%" + v + "%"
|
||||
}
|
||||
|
||||
func WrapLikeLeft(v string) string {
|
||||
return "%" + v
|
||||
}
|
||||
|
||||
func WrapLikeRight(v string) string {
|
||||
return "%" + v
|
||||
}
|
||||
|
||||
func Provide(...opt.Option) error {
|
||||
return container.Container.Provide(func(db *gorm.DB) contracts.Initial {
|
||||
models.SetDefault(db)
|
||||
return models.Q
|
||||
}, atom.GroupInitial)
|
||||
}
|
||||
|
||||
func DefaultProvider() container.ProviderContainer {
|
||||
return container.ProviderContainer{
|
||||
Provider: Provide,
|
||||
Options: []opt.Option{},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE medias(
|
||||
id SERIAL8 PRIMARY KEY,
|
||||
created_at timestamp NOT NULL DEFAULT now(),
|
||||
name varchar(255) NOT NULL DEFAULT '',
|
||||
mime_type varchar(128) NOT NULL DEFAULT '',
|
||||
size int8 NOT NULL DEFAULT 0,
|
||||
path varchar(255) NOT NULL DEFAULT '',
|
||||
metas jsonb NOT NULL DEFAULT '{}' ::jsonb,
|
||||
hash varchar(64) NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE medias;
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,26 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE posts(
|
||||
id SERIAL8 PRIMARY KEY,
|
||||
created_at timestamp NOT NULL DEFAULT now(),
|
||||
updated_at timestamp NOT NULL DEFAULT now(),
|
||||
deleted_at timestamp,
|
||||
status int2 NOT NULL DEFAULT 0,
|
||||
title varchar(128) NOT NULL,
|
||||
head_images jsonb DEFAULT '[]' ::jsonb NOT NULL,
|
||||
description varchar(256) NOT NULL,
|
||||
content text NOT NULL,
|
||||
price int8 NOT NULL DEFAULT 0,
|
||||
discount int2 NOT NULL DEFAULT 100,
|
||||
views int8 NOT NULL DEFAULT 0,
|
||||
likes int8 NOT NULL DEFAULT 0,
|
||||
tags jsonb DEFAULT '{}' ::jsonb,
|
||||
assets jsonb DEFAULT '{}' ::jsonb
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE posts;
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,22 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE users(
|
||||
id SERIAL8 PRIMARY KEY,
|
||||
created_at timestamp NOT NULL DEFAULT now(),
|
||||
updated_at timestamp NOT NULL DEFAULT now(),
|
||||
deleted_at timestamp,
|
||||
status int2 NOT NULL DEFAULT 0,
|
||||
open_id varchar(128) NOT NULL UNIQUE,
|
||||
username varchar(128) NOT NULL,
|
||||
avatar text
|
||||
);
|
||||
|
||||
SELECT
|
||||
setval('users_id_seq', 1000);
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE users;
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,18 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE user_posts(
|
||||
id SERIAL8 PRIMARY KEY,
|
||||
created_at timestamp NOT NULL DEFAULT now(),
|
||||
updated_at timestamp NOT NULL DEFAULT now(),
|
||||
--
|
||||
user_id int8 NOT NULL,
|
||||
post_id int8 NOT NULL,
|
||||
price int8 NOT NULL
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE user_posts;
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,26 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE orders(
|
||||
id SERIAL8 PRIMARY KEY,
|
||||
created_at timestamp NOT NULL DEFAULT now(),
|
||||
updated_at timestamp NOT NULL DEFAULT now(),
|
||||
order_no varchar(64) NOT NULL,
|
||||
sub_order_no varchar(64) NOT NULL DEFAULT '',
|
||||
transaction_id varchar(64) NOT NULL DEFAULT '',
|
||||
refund_transaction_id varchar(64) NOT NULL DEFAULT '',
|
||||
price int8 NOT NULL DEFAULT 0,
|
||||
discount int2 NOT NULL DEFAULT 100,
|
||||
currency varchar(10) NOT NULL DEFAULT 'CNY',
|
||||
payment_method varchar(50) NOT NULL DEFAULT 'wechatpay',
|
||||
post_id int8 NOT NULL,
|
||||
user_id int8 NOT NULL,
|
||||
status int2 NOT NULL,
|
||||
meta jsonb NOT NULL DEFAULT '{}' ::jsonb
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE orders;
|
||||
|
||||
-- +goose StatementEnd
|
||||
18
backend_v1/database/migrations/20250430014015_alter_user.sql
Normal file
18
backend_v1/database/migrations/20250430014015_alter_user.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE public.users
|
||||
ADD metas jsonb DEFAULT '{}'::jsonb NOT NULL;
|
||||
|
||||
ALTER TABLE public.users
|
||||
ADD auth_token jsonb DEFAULT '{}'::jsonb NOT NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE public.users
|
||||
DROP COLUMN metas;
|
||||
|
||||
ALTER TABLE public.users
|
||||
DROP COLUMN auth_token;
|
||||
|
||||
-- +goose StatementEnd
|
||||
11
backend_v1/database/migrations/20250512113213_alter_user.sql
Normal file
11
backend_v1/database/migrations/20250512113213_alter_user.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE public.users
|
||||
ADD balance int8 DEFAULT 0 NOT NULL;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE public.users
|
||||
DROP COLUMN balance;
|
||||
-- +goose StatementEnd
|
||||
59
backend_v1/database/models/medias.gen.go
Normal file
59
backend_v1/database/models/medias.gen.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
)
|
||||
|
||||
const TableNameMedia = "medias"
|
||||
|
||||
// Media mapped from table <medias>
|
||||
type Media struct {
|
||||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp without time zone;not null;default:now()" json:"created_at"`
|
||||
Name string `gorm:"column:name;type:character varying(255);not null" json:"name"`
|
||||
MimeType string `gorm:"column:mime_type;type:character varying(128);not null" json:"mime_type"`
|
||||
Size int64 `gorm:"column:size;type:bigint;not null" json:"size"`
|
||||
Path string `gorm:"column:path;type:character varying(255);not null" json:"path"`
|
||||
Metas types.JSON `gorm:"column:metas;type:jsonb;not null;default:{}" json:"metas"`
|
||||
Hash string `gorm:"column:hash;type:character varying(64);not null" json:"hash"`
|
||||
}
|
||||
|
||||
// Quick operations without importing query package
|
||||
// Update applies changed fields to the database using the default DB.
|
||||
func (m *Media) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Media.WithContext(ctx).Updates(m)
|
||||
}
|
||||
|
||||
// Save upserts the model using the default DB.
|
||||
func (m *Media) Save(ctx context.Context) error { return Q.Media.WithContext(ctx).Save(m) }
|
||||
|
||||
// Create inserts the model using the default DB.
|
||||
func (m *Media) Create(ctx context.Context) error { return Q.Media.WithContext(ctx).Create(m) }
|
||||
|
||||
// Delete removes the row represented by the model using the default DB.
|
||||
func (m *Media) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Media.WithContext(ctx).Delete(m)
|
||||
}
|
||||
|
||||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||||
func (m *Media) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Media.WithContext(ctx).Unscoped().Delete(m)
|
||||
}
|
||||
|
||||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||||
func (m *Media) Reload(ctx context.Context) error {
|
||||
fresh, err := Q.Media.WithContext(ctx).GetByID(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*m = *fresh
|
||||
return nil
|
||||
}
|
||||
487
backend_v1/database/models/medias.query.gen.go
Normal file
487
backend_v1/database/models/medias.query.gen.go
Normal file
@@ -0,0 +1,487 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
func newMedia(db *gorm.DB, opts ...gen.DOOption) mediaQuery {
|
||||
_mediaQuery := mediaQuery{}
|
||||
|
||||
_mediaQuery.mediaQueryDo.UseDB(db, opts...)
|
||||
_mediaQuery.mediaQueryDo.UseModel(&Media{})
|
||||
|
||||
tableName := _mediaQuery.mediaQueryDo.TableName()
|
||||
_mediaQuery.ALL = field.NewAsterisk(tableName)
|
||||
_mediaQuery.ID = field.NewInt64(tableName, "id")
|
||||
_mediaQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_mediaQuery.Name = field.NewString(tableName, "name")
|
||||
_mediaQuery.MimeType = field.NewString(tableName, "mime_type")
|
||||
_mediaQuery.Size = field.NewInt64(tableName, "size")
|
||||
_mediaQuery.Path = field.NewString(tableName, "path")
|
||||
_mediaQuery.Metas = field.NewJSONB(tableName, "metas")
|
||||
_mediaQuery.Hash = field.NewString(tableName, "hash")
|
||||
|
||||
_mediaQuery.fillFieldMap()
|
||||
|
||||
return _mediaQuery
|
||||
}
|
||||
|
||||
type mediaQuery struct {
|
||||
mediaQueryDo mediaQueryDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
CreatedAt field.Time
|
||||
Name field.String
|
||||
MimeType field.String
|
||||
Size field.Int64
|
||||
Path field.String
|
||||
Metas field.JSONB
|
||||
Hash field.String
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (m mediaQuery) Table(newTableName string) *mediaQuery {
|
||||
m.mediaQueryDo.UseTable(newTableName)
|
||||
return m.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (m mediaQuery) As(alias string) *mediaQuery {
|
||||
m.mediaQueryDo.DO = *(m.mediaQueryDo.As(alias).(*gen.DO))
|
||||
return m.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (m *mediaQuery) updateTableName(table string) *mediaQuery {
|
||||
m.ALL = field.NewAsterisk(table)
|
||||
m.ID = field.NewInt64(table, "id")
|
||||
m.CreatedAt = field.NewTime(table, "created_at")
|
||||
m.Name = field.NewString(table, "name")
|
||||
m.MimeType = field.NewString(table, "mime_type")
|
||||
m.Size = field.NewInt64(table, "size")
|
||||
m.Path = field.NewString(table, "path")
|
||||
m.Metas = field.NewJSONB(table, "metas")
|
||||
m.Hash = field.NewString(table, "hash")
|
||||
|
||||
m.fillFieldMap()
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *mediaQuery) QueryContext(ctx context.Context) (*mediaQuery, *mediaQueryDo) {
|
||||
return m, m.mediaQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (m *mediaQuery) WithContext(ctx context.Context) *mediaQueryDo {
|
||||
return m.mediaQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (m mediaQuery) TableName() string { return m.mediaQueryDo.TableName() }
|
||||
|
||||
func (m mediaQuery) Alias() string { return m.mediaQueryDo.Alias() }
|
||||
|
||||
func (m mediaQuery) Columns(cols ...field.Expr) gen.Columns { return m.mediaQueryDo.Columns(cols...) }
|
||||
|
||||
func (m *mediaQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := m.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (m *mediaQuery) fillFieldMap() {
|
||||
m.fieldMap = make(map[string]field.Expr, 8)
|
||||
m.fieldMap["id"] = m.ID
|
||||
m.fieldMap["created_at"] = m.CreatedAt
|
||||
m.fieldMap["name"] = m.Name
|
||||
m.fieldMap["mime_type"] = m.MimeType
|
||||
m.fieldMap["size"] = m.Size
|
||||
m.fieldMap["path"] = m.Path
|
||||
m.fieldMap["metas"] = m.Metas
|
||||
m.fieldMap["hash"] = m.Hash
|
||||
}
|
||||
|
||||
func (m mediaQuery) clone(db *gorm.DB) mediaQuery {
|
||||
m.mediaQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return m
|
||||
}
|
||||
|
||||
func (m mediaQuery) replaceDB(db *gorm.DB) mediaQuery {
|
||||
m.mediaQueryDo.ReplaceDB(db)
|
||||
return m
|
||||
}
|
||||
|
||||
type mediaQueryDo struct{ gen.DO }
|
||||
|
||||
func (m mediaQueryDo) Debug() *mediaQueryDo {
|
||||
return m.withDO(m.DO.Debug())
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) WithContext(ctx context.Context) *mediaQueryDo {
|
||||
return m.withDO(m.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) ReadDB() *mediaQueryDo {
|
||||
return m.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) WriteDB() *mediaQueryDo {
|
||||
return m.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Session(config *gorm.Session) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Session(config))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Clauses(conds ...clause.Expression) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Returning(value interface{}, columns ...string) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Not(conds ...gen.Condition) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Or(conds ...gen.Condition) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Select(conds ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Where(conds ...gen.Condition) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Order(conds ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Distinct(cols ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Omit(cols ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Join(table schema.Tabler, on ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) LeftJoin(table schema.Tabler, on ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) RightJoin(table schema.Tabler, on ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Group(cols ...field.Expr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Having(conds ...gen.Condition) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Limit(limit int) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Offset(offset int) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Unscoped() *mediaQueryDo {
|
||||
return m.withDO(m.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Create(values ...*Media) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return m.DO.Create(values)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) CreateInBatches(values []*Media, batchSize int) error {
|
||||
return m.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (m mediaQueryDo) Save(values ...*Media) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return m.DO.Save(values)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) First() (*Media, error) {
|
||||
if result, err := m.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Media), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Take() (*Media, error) {
|
||||
if result, err := m.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Media), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Last() (*Media, error) {
|
||||
if result, err := m.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Media), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Find() ([]*Media, error) {
|
||||
result, err := m.DO.Find()
|
||||
return result.([]*Media), err
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*Media, err error) {
|
||||
buf := make([]*Media, 0, batchSize)
|
||||
err = m.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) FindInBatches(result *[]*Media, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return m.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Attrs(attrs ...field.AssignExpr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Assign(attrs ...field.AssignExpr) *mediaQueryDo {
|
||||
return m.withDO(m.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Joins(fields ...field.RelationField) *mediaQueryDo {
|
||||
for _, _f := range fields {
|
||||
m = *m.withDO(m.DO.Joins(_f))
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Preload(fields ...field.RelationField) *mediaQueryDo {
|
||||
for _, _f := range fields {
|
||||
m = *m.withDO(m.DO.Preload(_f))
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) FirstOrInit() (*Media, error) {
|
||||
if result, err := m.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Media), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) FirstOrCreate() (*Media, error) {
|
||||
if result, err := m.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Media), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) FindByPage(offset int, limit int) (result []*Media, count int64, err error) {
|
||||
result, err = m.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = m.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = m.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = m.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Scan(result interface{}) (err error) {
|
||||
return m.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (m mediaQueryDo) Delete(models ...*Media) (result gen.ResultInfo, err error) {
|
||||
return m.DO.Delete(models)
|
||||
}
|
||||
|
||||
// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
|
||||
func (m mediaQueryDo) ForceDelete() (gen.ResultInfo, error) {
|
||||
return m.Unscoped().Delete()
|
||||
}
|
||||
|
||||
// Inc increases the given column by step for current scope.
|
||||
func (m mediaQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column + step
|
||||
e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
|
||||
return m.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Dec decreases the given column by step for current scope.
|
||||
func (m mediaQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column - step
|
||||
e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
|
||||
return m.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Sum returns SUM(column) for current scope.
|
||||
func (m mediaQueryDo) Sum(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
|
||||
if err := m.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Avg returns AVG(column) for current scope.
|
||||
func (m mediaQueryDo) Avg(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
|
||||
if err := m.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Min returns MIN(column) for current scope.
|
||||
func (m mediaQueryDo) Min(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
|
||||
if err := m.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Max returns MAX(column) for current scope.
|
||||
func (m mediaQueryDo) Max(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
|
||||
if err := m.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// PluckMap returns a map[key]value for selected key/value expressions within current scope.
|
||||
func (m mediaQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
|
||||
do := m.Select(key, val)
|
||||
rows, err := do.DO.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
mm := make(map[interface{}]interface{})
|
||||
for rows.Next() {
|
||||
var k interface{}
|
||||
var v interface{}
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
return mm, rows.Err()
|
||||
}
|
||||
|
||||
// Exists returns true if any record matches the given conditions.
|
||||
func (m mediaQueryDo) Exists(conds ...gen.Condition) (bool, error) {
|
||||
cnt, err := m.Where(conds...).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// PluckIDs returns all primary key values under current scope.
|
||||
func (m mediaQueryDo) PluckIDs() ([]int64, error) {
|
||||
ids := make([]int64, 0, 16)
|
||||
pk := field.NewInt64(m.TableName(), "id")
|
||||
if err := m.DO.Pluck(pk, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetByID finds a single record by primary key.
|
||||
func (m mediaQueryDo) GetByID(id int64) (*Media, error) {
|
||||
pk := field.NewInt64(m.TableName(), "id")
|
||||
return m.Where(pk.Eq(id)).First()
|
||||
}
|
||||
|
||||
// GetByIDs finds records by primary key list.
|
||||
func (m mediaQueryDo) GetByIDs(ids ...int64) ([]*Media, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*Media{}, nil
|
||||
}
|
||||
pk := field.NewInt64(m.TableName(), "id")
|
||||
return m.Where(pk.In(ids...)).Find()
|
||||
}
|
||||
|
||||
// DeleteByID deletes records by primary key.
|
||||
func (m mediaQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(m.TableName(), "id")
|
||||
return m.Where(pk.Eq(id)).Delete()
|
||||
}
|
||||
|
||||
// DeleteByIDs deletes records by a list of primary keys.
|
||||
func (m mediaQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
|
||||
if len(ids) == 0 {
|
||||
return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
|
||||
}
|
||||
pk := field.NewInt64(m.TableName(), "id")
|
||||
return m.Where(pk.In(ids...)).Delete()
|
||||
}
|
||||
|
||||
func (m *mediaQueryDo) withDO(do gen.Dao) *mediaQueryDo {
|
||||
m.DO = *do.(*gen.DO)
|
||||
return m
|
||||
}
|
||||
66
backend_v1/database/models/orders.gen.go
Normal file
66
backend_v1/database/models/orders.gen.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
)
|
||||
|
||||
const TableNameOrder = "orders"
|
||||
|
||||
// Order mapped from table <orders>
|
||||
type Order struct {
|
||||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp without time zone;not null;default:now()" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp without time zone;not null;default:now()" json:"updated_at"`
|
||||
OrderNo string `gorm:"column:order_no;type:character varying(64);not null" json:"order_no"`
|
||||
SubOrderNo string `gorm:"column:sub_order_no;type:character varying(64);not null" json:"sub_order_no"`
|
||||
TransactionID string `gorm:"column:transaction_id;type:character varying(64);not null" json:"transaction_id"`
|
||||
RefundTransactionID string `gorm:"column:refund_transaction_id;type:character varying(64);not null" json:"refund_transaction_id"`
|
||||
Price int64 `gorm:"column:price;type:bigint;not null" json:"price"`
|
||||
Discount int16 `gorm:"column:discount;type:smallint;not null;default:100" json:"discount"`
|
||||
Currency string `gorm:"column:currency;type:character varying(10);not null;default:CNY" json:"currency"`
|
||||
PaymentMethod string `gorm:"column:payment_method;type:character varying(50);not null;default:wechatpay" json:"payment_method"`
|
||||
PostID int64 `gorm:"column:post_id;type:bigint;not null" json:"post_id"`
|
||||
UserID int64 `gorm:"column:user_id;type:bigint;not null" json:"user_id"`
|
||||
Status int16 `gorm:"column:status;type:smallint;not null" json:"status"`
|
||||
Meta types.JSON `gorm:"column:meta;type:jsonb;not null;default:{}" json:"meta"`
|
||||
}
|
||||
|
||||
// Quick operations without importing query package
|
||||
// Update applies changed fields to the database using the default DB.
|
||||
func (m *Order) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Order.WithContext(ctx).Updates(m)
|
||||
}
|
||||
|
||||
// Save upserts the model using the default DB.
|
||||
func (m *Order) Save(ctx context.Context) error { return Q.Order.WithContext(ctx).Save(m) }
|
||||
|
||||
// Create inserts the model using the default DB.
|
||||
func (m *Order) Create(ctx context.Context) error { return Q.Order.WithContext(ctx).Create(m) }
|
||||
|
||||
// Delete removes the row represented by the model using the default DB.
|
||||
func (m *Order) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Order.WithContext(ctx).Delete(m)
|
||||
}
|
||||
|
||||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||||
func (m *Order) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Order.WithContext(ctx).Unscoped().Delete(m)
|
||||
}
|
||||
|
||||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||||
func (m *Order) Reload(ctx context.Context) error {
|
||||
fresh, err := Q.Order.WithContext(ctx).GetByID(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*m = *fresh
|
||||
return nil
|
||||
}
|
||||
515
backend_v1/database/models/orders.query.gen.go
Normal file
515
backend_v1/database/models/orders.query.gen.go
Normal file
@@ -0,0 +1,515 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
func newOrder(db *gorm.DB, opts ...gen.DOOption) orderQuery {
|
||||
_orderQuery := orderQuery{}
|
||||
|
||||
_orderQuery.orderQueryDo.UseDB(db, opts...)
|
||||
_orderQuery.orderQueryDo.UseModel(&Order{})
|
||||
|
||||
tableName := _orderQuery.orderQueryDo.TableName()
|
||||
_orderQuery.ALL = field.NewAsterisk(tableName)
|
||||
_orderQuery.ID = field.NewInt64(tableName, "id")
|
||||
_orderQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_orderQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_orderQuery.OrderNo = field.NewString(tableName, "order_no")
|
||||
_orderQuery.SubOrderNo = field.NewString(tableName, "sub_order_no")
|
||||
_orderQuery.TransactionID = field.NewString(tableName, "transaction_id")
|
||||
_orderQuery.RefundTransactionID = field.NewString(tableName, "refund_transaction_id")
|
||||
_orderQuery.Price = field.NewInt64(tableName, "price")
|
||||
_orderQuery.Discount = field.NewInt16(tableName, "discount")
|
||||
_orderQuery.Currency = field.NewString(tableName, "currency")
|
||||
_orderQuery.PaymentMethod = field.NewString(tableName, "payment_method")
|
||||
_orderQuery.PostID = field.NewInt64(tableName, "post_id")
|
||||
_orderQuery.UserID = field.NewInt64(tableName, "user_id")
|
||||
_orderQuery.Status = field.NewInt16(tableName, "status")
|
||||
_orderQuery.Meta = field.NewJSONB(tableName, "meta")
|
||||
|
||||
_orderQuery.fillFieldMap()
|
||||
|
||||
return _orderQuery
|
||||
}
|
||||
|
||||
type orderQuery struct {
|
||||
orderQueryDo orderQueryDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
OrderNo field.String
|
||||
SubOrderNo field.String
|
||||
TransactionID field.String
|
||||
RefundTransactionID field.String
|
||||
Price field.Int64
|
||||
Discount field.Int16
|
||||
Currency field.String
|
||||
PaymentMethod field.String
|
||||
PostID field.Int64
|
||||
UserID field.Int64
|
||||
Status field.Int16
|
||||
Meta field.JSONB
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (o orderQuery) Table(newTableName string) *orderQuery {
|
||||
o.orderQueryDo.UseTable(newTableName)
|
||||
return o.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (o orderQuery) As(alias string) *orderQuery {
|
||||
o.orderQueryDo.DO = *(o.orderQueryDo.As(alias).(*gen.DO))
|
||||
return o.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (o *orderQuery) updateTableName(table string) *orderQuery {
|
||||
o.ALL = field.NewAsterisk(table)
|
||||
o.ID = field.NewInt64(table, "id")
|
||||
o.CreatedAt = field.NewTime(table, "created_at")
|
||||
o.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
o.OrderNo = field.NewString(table, "order_no")
|
||||
o.SubOrderNo = field.NewString(table, "sub_order_no")
|
||||
o.TransactionID = field.NewString(table, "transaction_id")
|
||||
o.RefundTransactionID = field.NewString(table, "refund_transaction_id")
|
||||
o.Price = field.NewInt64(table, "price")
|
||||
o.Discount = field.NewInt16(table, "discount")
|
||||
o.Currency = field.NewString(table, "currency")
|
||||
o.PaymentMethod = field.NewString(table, "payment_method")
|
||||
o.PostID = field.NewInt64(table, "post_id")
|
||||
o.UserID = field.NewInt64(table, "user_id")
|
||||
o.Status = field.NewInt16(table, "status")
|
||||
o.Meta = field.NewJSONB(table, "meta")
|
||||
|
||||
o.fillFieldMap()
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *orderQuery) QueryContext(ctx context.Context) (*orderQuery, *orderQueryDo) {
|
||||
return o, o.orderQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (o *orderQuery) WithContext(ctx context.Context) *orderQueryDo {
|
||||
return o.orderQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (o orderQuery) TableName() string { return o.orderQueryDo.TableName() }
|
||||
|
||||
func (o orderQuery) Alias() string { return o.orderQueryDo.Alias() }
|
||||
|
||||
func (o orderQuery) Columns(cols ...field.Expr) gen.Columns { return o.orderQueryDo.Columns(cols...) }
|
||||
|
||||
func (o *orderQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := o.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (o *orderQuery) fillFieldMap() {
|
||||
o.fieldMap = make(map[string]field.Expr, 15)
|
||||
o.fieldMap["id"] = o.ID
|
||||
o.fieldMap["created_at"] = o.CreatedAt
|
||||
o.fieldMap["updated_at"] = o.UpdatedAt
|
||||
o.fieldMap["order_no"] = o.OrderNo
|
||||
o.fieldMap["sub_order_no"] = o.SubOrderNo
|
||||
o.fieldMap["transaction_id"] = o.TransactionID
|
||||
o.fieldMap["refund_transaction_id"] = o.RefundTransactionID
|
||||
o.fieldMap["price"] = o.Price
|
||||
o.fieldMap["discount"] = o.Discount
|
||||
o.fieldMap["currency"] = o.Currency
|
||||
o.fieldMap["payment_method"] = o.PaymentMethod
|
||||
o.fieldMap["post_id"] = o.PostID
|
||||
o.fieldMap["user_id"] = o.UserID
|
||||
o.fieldMap["status"] = o.Status
|
||||
o.fieldMap["meta"] = o.Meta
|
||||
}
|
||||
|
||||
func (o orderQuery) clone(db *gorm.DB) orderQuery {
|
||||
o.orderQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return o
|
||||
}
|
||||
|
||||
func (o orderQuery) replaceDB(db *gorm.DB) orderQuery {
|
||||
o.orderQueryDo.ReplaceDB(db)
|
||||
return o
|
||||
}
|
||||
|
||||
type orderQueryDo struct{ gen.DO }
|
||||
|
||||
func (o orderQueryDo) Debug() *orderQueryDo {
|
||||
return o.withDO(o.DO.Debug())
|
||||
}
|
||||
|
||||
func (o orderQueryDo) WithContext(ctx context.Context) *orderQueryDo {
|
||||
return o.withDO(o.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) ReadDB() *orderQueryDo {
|
||||
return o.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) WriteDB() *orderQueryDo {
|
||||
return o.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Session(config *gorm.Session) *orderQueryDo {
|
||||
return o.withDO(o.DO.Session(config))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Clauses(conds ...clause.Expression) *orderQueryDo {
|
||||
return o.withDO(o.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Returning(value interface{}, columns ...string) *orderQueryDo {
|
||||
return o.withDO(o.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Not(conds ...gen.Condition) *orderQueryDo {
|
||||
return o.withDO(o.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Or(conds ...gen.Condition) *orderQueryDo {
|
||||
return o.withDO(o.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Select(conds ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Where(conds ...gen.Condition) *orderQueryDo {
|
||||
return o.withDO(o.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Order(conds ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Distinct(cols ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Omit(cols ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Join(table schema.Tabler, on ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) LeftJoin(table schema.Tabler, on ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) RightJoin(table schema.Tabler, on ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Group(cols ...field.Expr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Having(conds ...gen.Condition) *orderQueryDo {
|
||||
return o.withDO(o.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Limit(limit int) *orderQueryDo {
|
||||
return o.withDO(o.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Offset(offset int) *orderQueryDo {
|
||||
return o.withDO(o.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *orderQueryDo {
|
||||
return o.withDO(o.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Unscoped() *orderQueryDo {
|
||||
return o.withDO(o.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Create(values ...*Order) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return o.DO.Create(values)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) CreateInBatches(values []*Order, batchSize int) error {
|
||||
return o.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (o orderQueryDo) Save(values ...*Order) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return o.DO.Save(values)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) First() (*Order, error) {
|
||||
if result, err := o.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Order), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Take() (*Order, error) {
|
||||
if result, err := o.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Order), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Last() (*Order, error) {
|
||||
if result, err := o.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Order), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Find() ([]*Order, error) {
|
||||
result, err := o.DO.Find()
|
||||
return result.([]*Order), err
|
||||
}
|
||||
|
||||
func (o orderQueryDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*Order, err error) {
|
||||
buf := make([]*Order, 0, batchSize)
|
||||
err = o.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (o orderQueryDo) FindInBatches(result *[]*Order, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return o.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Attrs(attrs ...field.AssignExpr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Assign(attrs ...field.AssignExpr) *orderQueryDo {
|
||||
return o.withDO(o.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Joins(fields ...field.RelationField) *orderQueryDo {
|
||||
for _, _f := range fields {
|
||||
o = *o.withDO(o.DO.Joins(_f))
|
||||
}
|
||||
return &o
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Preload(fields ...field.RelationField) *orderQueryDo {
|
||||
for _, _f := range fields {
|
||||
o = *o.withDO(o.DO.Preload(_f))
|
||||
}
|
||||
return &o
|
||||
}
|
||||
|
||||
func (o orderQueryDo) FirstOrInit() (*Order, error) {
|
||||
if result, err := o.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Order), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (o orderQueryDo) FirstOrCreate() (*Order, error) {
|
||||
if result, err := o.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Order), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (o orderQueryDo) FindByPage(offset int, limit int) (result []*Order, count int64, err error) {
|
||||
result, err = o.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = o.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (o orderQueryDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = o.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = o.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Scan(result interface{}) (err error) {
|
||||
return o.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (o orderQueryDo) Delete(models ...*Order) (result gen.ResultInfo, err error) {
|
||||
return o.DO.Delete(models)
|
||||
}
|
||||
|
||||
// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
|
||||
func (o orderQueryDo) ForceDelete() (gen.ResultInfo, error) {
|
||||
return o.Unscoped().Delete()
|
||||
}
|
||||
|
||||
// Inc increases the given column by step for current scope.
|
||||
func (o orderQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column + step
|
||||
e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
|
||||
return o.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Dec decreases the given column by step for current scope.
|
||||
func (o orderQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column - step
|
||||
e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
|
||||
return o.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Sum returns SUM(column) for current scope.
|
||||
func (o orderQueryDo) Sum(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
|
||||
if err := o.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Avg returns AVG(column) for current scope.
|
||||
func (o orderQueryDo) Avg(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
|
||||
if err := o.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Min returns MIN(column) for current scope.
|
||||
func (o orderQueryDo) Min(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
|
||||
if err := o.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Max returns MAX(column) for current scope.
|
||||
func (o orderQueryDo) Max(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
|
||||
if err := o.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// PluckMap returns a map[key]value for selected key/value expressions within current scope.
|
||||
func (o orderQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
|
||||
do := o.Select(key, val)
|
||||
rows, err := do.DO.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
mm := make(map[interface{}]interface{})
|
||||
for rows.Next() {
|
||||
var k interface{}
|
||||
var v interface{}
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
return mm, rows.Err()
|
||||
}
|
||||
|
||||
// Exists returns true if any record matches the given conditions.
|
||||
func (o orderQueryDo) Exists(conds ...gen.Condition) (bool, error) {
|
||||
cnt, err := o.Where(conds...).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// PluckIDs returns all primary key values under current scope.
|
||||
func (o orderQueryDo) PluckIDs() ([]int64, error) {
|
||||
ids := make([]int64, 0, 16)
|
||||
pk := field.NewInt64(o.TableName(), "id")
|
||||
if err := o.DO.Pluck(pk, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetByID finds a single record by primary key.
|
||||
func (o orderQueryDo) GetByID(id int64) (*Order, error) {
|
||||
pk := field.NewInt64(o.TableName(), "id")
|
||||
return o.Where(pk.Eq(id)).First()
|
||||
}
|
||||
|
||||
// GetByIDs finds records by primary key list.
|
||||
func (o orderQueryDo) GetByIDs(ids ...int64) ([]*Order, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*Order{}, nil
|
||||
}
|
||||
pk := field.NewInt64(o.TableName(), "id")
|
||||
return o.Where(pk.In(ids...)).Find()
|
||||
}
|
||||
|
||||
// DeleteByID deletes records by primary key.
|
||||
func (o orderQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(o.TableName(), "id")
|
||||
return o.Where(pk.Eq(id)).Delete()
|
||||
}
|
||||
|
||||
// DeleteByIDs deletes records by a list of primary keys.
|
||||
func (o orderQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
|
||||
if len(ids) == 0 {
|
||||
return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
|
||||
}
|
||||
pk := field.NewInt64(o.TableName(), "id")
|
||||
return o.Where(pk.In(ids...)).Delete()
|
||||
}
|
||||
|
||||
func (o *orderQueryDo) withDO(do gen.Dao) *orderQueryDo {
|
||||
o.DO = *do.(*gen.DO)
|
||||
return o
|
||||
}
|
||||
72
backend_v1/database/models/posts.gen.go
Normal file
72
backend_v1/database/models/posts.gen.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const TableNamePost = "posts"
|
||||
|
||||
// Post mapped from table <posts>
|
||||
type Post struct {
|
||||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp without time zone;not null;default:now()" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp without time zone;not null;default:now()" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:timestamp without time zone" json:"deleted_at"`
|
||||
Status int16 `gorm:"column:status;type:smallint;not null" json:"status"`
|
||||
Title string `gorm:"column:title;type:character varying(128);not null" json:"title"`
|
||||
HeadImages types.JSON `gorm:"column:head_images;type:jsonb;not null;default:[]" json:"head_images"`
|
||||
Description string `gorm:"column:description;type:character varying(256);not null" json:"description"`
|
||||
Content string `gorm:"column:content;type:text;not null" json:"content"`
|
||||
Price int64 `gorm:"column:price;type:bigint;not null" json:"price"`
|
||||
Discount int16 `gorm:"column:discount;type:smallint;not null;default:100" json:"discount"`
|
||||
Views int64 `gorm:"column:views;type:bigint;not null" json:"views"`
|
||||
Likes int64 `gorm:"column:likes;type:bigint;not null" json:"likes"`
|
||||
Tags types.JSON `gorm:"column:tags;type:jsonb;default:{}" json:"tags"`
|
||||
Assets types.JSON `gorm:"column:assets;type:jsonb;default:{}" json:"assets"`
|
||||
}
|
||||
|
||||
// Quick operations without importing query package
|
||||
// Update applies changed fields to the database using the default DB.
|
||||
func (m *Post) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Post.WithContext(ctx).Updates(m)
|
||||
}
|
||||
|
||||
// Save upserts the model using the default DB.
|
||||
func (m *Post) Save(ctx context.Context) error { return Q.Post.WithContext(ctx).Save(m) }
|
||||
|
||||
// Create inserts the model using the default DB.
|
||||
func (m *Post) Create(ctx context.Context) error { return Q.Post.WithContext(ctx).Create(m) }
|
||||
|
||||
// Delete removes the row represented by the model using the default DB.
|
||||
func (m *Post) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Post.WithContext(ctx).Delete(m)
|
||||
}
|
||||
|
||||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||||
func (m *Post) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Post.WithContext(ctx).Unscoped().Delete(m)
|
||||
}
|
||||
|
||||
// Restore sets deleted_at to NULL for this model's primary key using the default DB.
|
||||
func (m *Post) Restore(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.Post.WithContext(ctx).RestoreByID(m.ID)
|
||||
}
|
||||
|
||||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||||
func (m *Post) Reload(ctx context.Context) error {
|
||||
fresh, err := Q.Post.WithContext(ctx).GetByID(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*m = *fresh
|
||||
return nil
|
||||
}
|
||||
528
backend_v1/database/models/posts.query.gen.go
Normal file
528
backend_v1/database/models/posts.query.gen.go
Normal file
@@ -0,0 +1,528 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
func newPost(db *gorm.DB, opts ...gen.DOOption) postQuery {
|
||||
_postQuery := postQuery{}
|
||||
|
||||
_postQuery.postQueryDo.UseDB(db, opts...)
|
||||
_postQuery.postQueryDo.UseModel(&Post{})
|
||||
|
||||
tableName := _postQuery.postQueryDo.TableName()
|
||||
_postQuery.ALL = field.NewAsterisk(tableName)
|
||||
_postQuery.ID = field.NewInt64(tableName, "id")
|
||||
_postQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_postQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_postQuery.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
_postQuery.Status = field.NewInt16(tableName, "status")
|
||||
_postQuery.Title = field.NewString(tableName, "title")
|
||||
_postQuery.HeadImages = field.NewJSONB(tableName, "head_images")
|
||||
_postQuery.Description = field.NewString(tableName, "description")
|
||||
_postQuery.Content = field.NewString(tableName, "content")
|
||||
_postQuery.Price = field.NewInt64(tableName, "price")
|
||||
_postQuery.Discount = field.NewInt16(tableName, "discount")
|
||||
_postQuery.Views = field.NewInt64(tableName, "views")
|
||||
_postQuery.Likes = field.NewInt64(tableName, "likes")
|
||||
_postQuery.Tags = field.NewJSONB(tableName, "tags")
|
||||
_postQuery.Assets = field.NewJSONB(tableName, "assets")
|
||||
|
||||
_postQuery.fillFieldMap()
|
||||
|
||||
return _postQuery
|
||||
}
|
||||
|
||||
type postQuery struct {
|
||||
postQueryDo postQueryDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
Status field.Int16
|
||||
Title field.String
|
||||
HeadImages field.JSONB
|
||||
Description field.String
|
||||
Content field.String
|
||||
Price field.Int64
|
||||
Discount field.Int16
|
||||
Views field.Int64
|
||||
Likes field.Int64
|
||||
Tags field.JSONB
|
||||
Assets field.JSONB
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (p postQuery) Table(newTableName string) *postQuery {
|
||||
p.postQueryDo.UseTable(newTableName)
|
||||
return p.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (p postQuery) As(alias string) *postQuery {
|
||||
p.postQueryDo.DO = *(p.postQueryDo.As(alias).(*gen.DO))
|
||||
return p.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (p *postQuery) updateTableName(table string) *postQuery {
|
||||
p.ALL = field.NewAsterisk(table)
|
||||
p.ID = field.NewInt64(table, "id")
|
||||
p.CreatedAt = field.NewTime(table, "created_at")
|
||||
p.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
p.DeletedAt = field.NewField(table, "deleted_at")
|
||||
p.Status = field.NewInt16(table, "status")
|
||||
p.Title = field.NewString(table, "title")
|
||||
p.HeadImages = field.NewJSONB(table, "head_images")
|
||||
p.Description = field.NewString(table, "description")
|
||||
p.Content = field.NewString(table, "content")
|
||||
p.Price = field.NewInt64(table, "price")
|
||||
p.Discount = field.NewInt16(table, "discount")
|
||||
p.Views = field.NewInt64(table, "views")
|
||||
p.Likes = field.NewInt64(table, "likes")
|
||||
p.Tags = field.NewJSONB(table, "tags")
|
||||
p.Assets = field.NewJSONB(table, "assets")
|
||||
|
||||
p.fillFieldMap()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *postQuery) QueryContext(ctx context.Context) (*postQuery, *postQueryDo) {
|
||||
return p, p.postQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (p *postQuery) WithContext(ctx context.Context) *postQueryDo {
|
||||
return p.postQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (p postQuery) TableName() string { return p.postQueryDo.TableName() }
|
||||
|
||||
func (p postQuery) Alias() string { return p.postQueryDo.Alias() }
|
||||
|
||||
func (p postQuery) Columns(cols ...field.Expr) gen.Columns { return p.postQueryDo.Columns(cols...) }
|
||||
|
||||
func (p *postQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := p.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (p *postQuery) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 15)
|
||||
p.fieldMap["id"] = p.ID
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
p.fieldMap["updated_at"] = p.UpdatedAt
|
||||
p.fieldMap["deleted_at"] = p.DeletedAt
|
||||
p.fieldMap["status"] = p.Status
|
||||
p.fieldMap["title"] = p.Title
|
||||
p.fieldMap["head_images"] = p.HeadImages
|
||||
p.fieldMap["description"] = p.Description
|
||||
p.fieldMap["content"] = p.Content
|
||||
p.fieldMap["price"] = p.Price
|
||||
p.fieldMap["discount"] = p.Discount
|
||||
p.fieldMap["views"] = p.Views
|
||||
p.fieldMap["likes"] = p.Likes
|
||||
p.fieldMap["tags"] = p.Tags
|
||||
p.fieldMap["assets"] = p.Assets
|
||||
}
|
||||
|
||||
func (p postQuery) clone(db *gorm.DB) postQuery {
|
||||
p.postQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p postQuery) replaceDB(db *gorm.DB) postQuery {
|
||||
p.postQueryDo.ReplaceDB(db)
|
||||
return p
|
||||
}
|
||||
|
||||
type postQueryDo struct{ gen.DO }
|
||||
|
||||
func (p postQueryDo) Debug() *postQueryDo {
|
||||
return p.withDO(p.DO.Debug())
|
||||
}
|
||||
|
||||
func (p postQueryDo) WithContext(ctx context.Context) *postQueryDo {
|
||||
return p.withDO(p.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (p postQueryDo) ReadDB() *postQueryDo {
|
||||
return p.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (p postQueryDo) WriteDB() *postQueryDo {
|
||||
return p.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (p postQueryDo) Session(config *gorm.Session) *postQueryDo {
|
||||
return p.withDO(p.DO.Session(config))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Clauses(conds ...clause.Expression) *postQueryDo {
|
||||
return p.withDO(p.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Returning(value interface{}, columns ...string) *postQueryDo {
|
||||
return p.withDO(p.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Not(conds ...gen.Condition) *postQueryDo {
|
||||
return p.withDO(p.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Or(conds ...gen.Condition) *postQueryDo {
|
||||
return p.withDO(p.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Select(conds ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Where(conds ...gen.Condition) *postQueryDo {
|
||||
return p.withDO(p.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Order(conds ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Distinct(cols ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Omit(cols ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Join(table schema.Tabler, on ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) LeftJoin(table schema.Tabler, on ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) RightJoin(table schema.Tabler, on ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Group(cols ...field.Expr) *postQueryDo {
|
||||
return p.withDO(p.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Having(conds ...gen.Condition) *postQueryDo {
|
||||
return p.withDO(p.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Limit(limit int) *postQueryDo {
|
||||
return p.withDO(p.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Offset(offset int) *postQueryDo {
|
||||
return p.withDO(p.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *postQueryDo {
|
||||
return p.withDO(p.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Unscoped() *postQueryDo {
|
||||
return p.withDO(p.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (p postQueryDo) Create(values ...*Post) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Create(values)
|
||||
}
|
||||
|
||||
func (p postQueryDo) CreateInBatches(values []*Post, batchSize int) error {
|
||||
return p.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (p postQueryDo) Save(values ...*Post) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return p.DO.Save(values)
|
||||
}
|
||||
|
||||
func (p postQueryDo) First() (*Post, error) {
|
||||
if result, err := p.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Post), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p postQueryDo) Take() (*Post, error) {
|
||||
if result, err := p.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Post), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p postQueryDo) Last() (*Post, error) {
|
||||
if result, err := p.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Post), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p postQueryDo) Find() ([]*Post, error) {
|
||||
result, err := p.DO.Find()
|
||||
return result.([]*Post), err
|
||||
}
|
||||
|
||||
func (p postQueryDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*Post, err error) {
|
||||
buf := make([]*Post, 0, batchSize)
|
||||
err = p.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (p postQueryDo) FindInBatches(result *[]*Post, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return p.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (p postQueryDo) Attrs(attrs ...field.AssignExpr) *postQueryDo {
|
||||
return p.withDO(p.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Assign(attrs ...field.AssignExpr) *postQueryDo {
|
||||
return p.withDO(p.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (p postQueryDo) Joins(fields ...field.RelationField) *postQueryDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Joins(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p postQueryDo) Preload(fields ...field.RelationField) *postQueryDo {
|
||||
for _, _f := range fields {
|
||||
p = *p.withDO(p.DO.Preload(_f))
|
||||
}
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p postQueryDo) FirstOrInit() (*Post, error) {
|
||||
if result, err := p.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Post), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p postQueryDo) FirstOrCreate() (*Post, error) {
|
||||
if result, err := p.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*Post), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p postQueryDo) FindByPage(offset int, limit int) (result []*Post, count int64, err error) {
|
||||
result, err = p.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = p.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (p postQueryDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = p.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (p postQueryDo) Scan(result interface{}) (err error) {
|
||||
return p.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (p postQueryDo) Delete(models ...*Post) (result gen.ResultInfo, err error) {
|
||||
return p.DO.Delete(models)
|
||||
}
|
||||
|
||||
// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
|
||||
func (p postQueryDo) ForceDelete() (gen.ResultInfo, error) {
|
||||
return p.Unscoped().Delete()
|
||||
}
|
||||
|
||||
// Inc increases the given column by step for current scope.
|
||||
func (p postQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column + step
|
||||
e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
|
||||
return p.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Dec decreases the given column by step for current scope.
|
||||
func (p postQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column - step
|
||||
e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
|
||||
return p.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Sum returns SUM(column) for current scope.
|
||||
func (p postQueryDo) Sum(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
|
||||
if err := p.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Avg returns AVG(column) for current scope.
|
||||
func (p postQueryDo) Avg(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
|
||||
if err := p.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Min returns MIN(column) for current scope.
|
||||
func (p postQueryDo) Min(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
|
||||
if err := p.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Max returns MAX(column) for current scope.
|
||||
func (p postQueryDo) Max(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
|
||||
if err := p.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// PluckMap returns a map[key]value for selected key/value expressions within current scope.
|
||||
func (p postQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
|
||||
do := p.Select(key, val)
|
||||
rows, err := do.DO.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
mm := make(map[interface{}]interface{})
|
||||
for rows.Next() {
|
||||
var k interface{}
|
||||
var v interface{}
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
return mm, rows.Err()
|
||||
}
|
||||
|
||||
// Exists returns true if any record matches the given conditions.
|
||||
func (p postQueryDo) Exists(conds ...gen.Condition) (bool, error) {
|
||||
cnt, err := p.Where(conds...).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// PluckIDs returns all primary key values under current scope.
|
||||
func (p postQueryDo) PluckIDs() ([]int64, error) {
|
||||
ids := make([]int64, 0, 16)
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
if err := p.DO.Pluck(pk, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetByID finds a single record by primary key.
|
||||
func (p postQueryDo) GetByID(id int64) (*Post, error) {
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
return p.Where(pk.Eq(id)).First()
|
||||
}
|
||||
|
||||
// GetByIDs finds records by primary key list.
|
||||
func (p postQueryDo) GetByIDs(ids ...int64) ([]*Post, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*Post{}, nil
|
||||
}
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
return p.Where(pk.In(ids...)).Find()
|
||||
}
|
||||
|
||||
// DeleteByID deletes records by primary key.
|
||||
func (p postQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
return p.Where(pk.Eq(id)).Delete()
|
||||
}
|
||||
|
||||
// DeleteByIDs deletes records by a list of primary keys.
|
||||
func (p postQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
|
||||
if len(ids) == 0 {
|
||||
return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
|
||||
}
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
return p.Where(pk.In(ids...)).Delete()
|
||||
}
|
||||
|
||||
// RestoreWhere sets deleted_at to NULL for rows matching current scope + conds.
|
||||
func (p postQueryDo) RestoreWhere(conds ...gen.Condition) (gen.ResultInfo, error) {
|
||||
col := field.NewField(p.TableName(), "deleted_at")
|
||||
return p.Unscoped().Where(conds...).UpdateColumn(col, nil)
|
||||
}
|
||||
|
||||
// RestoreByID sets deleted_at to NULL for the given primary key.
|
||||
func (p postQueryDo) RestoreByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(p.TableName(), "id")
|
||||
col := field.NewField(p.TableName(), "deleted_at")
|
||||
return p.Unscoped().Where(pk.Eq(id)).UpdateColumn(col, nil)
|
||||
}
|
||||
|
||||
func (p *postQueryDo) withDO(do gen.Dao) *postQueryDo {
|
||||
p.DO = *do.(*gen.DO)
|
||||
return p
|
||||
}
|
||||
135
backend_v1/database/models/query.gen.go
Normal file
135
backend_v1/database/models/query.gen.go
Normal file
@@ -0,0 +1,135 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
var (
|
||||
Q = new(Query)
|
||||
MediaQuery *mediaQuery
|
||||
OrderQuery *orderQuery
|
||||
PostQuery *postQuery
|
||||
UserQuery *userQuery
|
||||
UserPostQuery *userPostQuery
|
||||
)
|
||||
|
||||
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
|
||||
*Q = *Use(db, opts...)
|
||||
MediaQuery = &Q.Media
|
||||
OrderQuery = &Q.Order
|
||||
PostQuery = &Q.Post
|
||||
UserQuery = &Q.User
|
||||
UserPostQuery = &Q.UserPost
|
||||
}
|
||||
|
||||
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
|
||||
return &Query{
|
||||
db: db,
|
||||
Media: newMedia(db, opts...),
|
||||
Order: newOrder(db, opts...),
|
||||
Post: newPost(db, opts...),
|
||||
User: newUser(db, opts...),
|
||||
UserPost: newUserPost(db, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
db *gorm.DB
|
||||
|
||||
Media mediaQuery
|
||||
Order orderQuery
|
||||
Post postQuery
|
||||
User userQuery
|
||||
UserPost userPostQuery
|
||||
}
|
||||
|
||||
func (q *Query) Available() bool { return q.db != nil }
|
||||
|
||||
func (q *Query) clone(db *gorm.DB) *Query {
|
||||
return &Query{
|
||||
db: db,
|
||||
Media: q.Media.clone(db),
|
||||
Order: q.Order.clone(db),
|
||||
Post: q.Post.clone(db),
|
||||
User: q.User.clone(db),
|
||||
UserPost: q.UserPost.clone(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Query) ReadDB() *Query {
|
||||
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
|
||||
}
|
||||
|
||||
func (q *Query) WriteDB() *Query {
|
||||
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
|
||||
}
|
||||
|
||||
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
|
||||
return &Query{
|
||||
db: db,
|
||||
Media: q.Media.replaceDB(db),
|
||||
Order: q.Order.replaceDB(db),
|
||||
Post: q.Post.replaceDB(db),
|
||||
User: q.User.replaceDB(db),
|
||||
UserPost: q.UserPost.replaceDB(db),
|
||||
}
|
||||
}
|
||||
|
||||
type queryCtx struct {
|
||||
Media *mediaQueryDo
|
||||
Order *orderQueryDo
|
||||
Post *postQueryDo
|
||||
User *userQueryDo
|
||||
UserPost *userPostQueryDo
|
||||
}
|
||||
|
||||
func (q *Query) WithContext(ctx context.Context) *queryCtx {
|
||||
return &queryCtx{
|
||||
Media: q.Media.WithContext(ctx),
|
||||
Order: q.Order.WithContext(ctx),
|
||||
Post: q.Post.WithContext(ctx),
|
||||
User: q.User.WithContext(ctx),
|
||||
UserPost: q.UserPost.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
|
||||
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
|
||||
}
|
||||
|
||||
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
|
||||
tx := q.db.Begin(opts...)
|
||||
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
|
||||
}
|
||||
|
||||
type QueryTx struct {
|
||||
*Query
|
||||
Error error
|
||||
}
|
||||
|
||||
func (q *QueryTx) Commit() error {
|
||||
return q.db.Commit().Error
|
||||
}
|
||||
|
||||
func (q *QueryTx) Rollback() error {
|
||||
return q.db.Rollback().Error
|
||||
}
|
||||
|
||||
func (q *QueryTx) SavePoint(name string) error {
|
||||
return q.db.SavePoint(name).Error
|
||||
}
|
||||
|
||||
func (q *QueryTx) RollbackTo(name string) error {
|
||||
return q.db.RollbackTo(name).Error
|
||||
}
|
||||
56
backend_v1/database/models/user_posts.gen.go
Normal file
56
backend_v1/database/models/user_posts.gen.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
const TableNameUserPost = "user_posts"
|
||||
|
||||
// UserPost mapped from table <user_posts>
|
||||
type UserPost struct {
|
||||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp without time zone;not null;default:now()" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp without time zone;not null;default:now()" json:"updated_at"`
|
||||
UserID int64 `gorm:"column:user_id;type:bigint;not null" json:"user_id"`
|
||||
PostID int64 `gorm:"column:post_id;type:bigint;not null" json:"post_id"`
|
||||
Price int64 `gorm:"column:price;type:bigint;not null" json:"price"`
|
||||
}
|
||||
|
||||
// Quick operations without importing query package
|
||||
// Update applies changed fields to the database using the default DB.
|
||||
func (m *UserPost) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.UserPost.WithContext(ctx).Updates(m)
|
||||
}
|
||||
|
||||
// Save upserts the model using the default DB.
|
||||
func (m *UserPost) Save(ctx context.Context) error { return Q.UserPost.WithContext(ctx).Save(m) }
|
||||
|
||||
// Create inserts the model using the default DB.
|
||||
func (m *UserPost) Create(ctx context.Context) error { return Q.UserPost.WithContext(ctx).Create(m) }
|
||||
|
||||
// Delete removes the row represented by the model using the default DB.
|
||||
func (m *UserPost) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.UserPost.WithContext(ctx).Delete(m)
|
||||
}
|
||||
|
||||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||||
func (m *UserPost) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.UserPost.WithContext(ctx).Unscoped().Delete(m)
|
||||
}
|
||||
|
||||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||||
func (m *UserPost) Reload(ctx context.Context) error {
|
||||
fresh, err := Q.UserPost.WithContext(ctx).GetByID(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*m = *fresh
|
||||
return nil
|
||||
}
|
||||
481
backend_v1/database/models/user_posts.query.gen.go
Normal file
481
backend_v1/database/models/user_posts.query.gen.go
Normal file
@@ -0,0 +1,481 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
func newUserPost(db *gorm.DB, opts ...gen.DOOption) userPostQuery {
|
||||
_userPostQuery := userPostQuery{}
|
||||
|
||||
_userPostQuery.userPostQueryDo.UseDB(db, opts...)
|
||||
_userPostQuery.userPostQueryDo.UseModel(&UserPost{})
|
||||
|
||||
tableName := _userPostQuery.userPostQueryDo.TableName()
|
||||
_userPostQuery.ALL = field.NewAsterisk(tableName)
|
||||
_userPostQuery.ID = field.NewInt64(tableName, "id")
|
||||
_userPostQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_userPostQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_userPostQuery.UserID = field.NewInt64(tableName, "user_id")
|
||||
_userPostQuery.PostID = field.NewInt64(tableName, "post_id")
|
||||
_userPostQuery.Price = field.NewInt64(tableName, "price")
|
||||
|
||||
_userPostQuery.fillFieldMap()
|
||||
|
||||
return _userPostQuery
|
||||
}
|
||||
|
||||
type userPostQuery struct {
|
||||
userPostQueryDo userPostQueryDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
UserID field.Int64
|
||||
PostID field.Int64
|
||||
Price field.Int64
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (u userPostQuery) Table(newTableName string) *userPostQuery {
|
||||
u.userPostQueryDo.UseTable(newTableName)
|
||||
return u.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (u userPostQuery) As(alias string) *userPostQuery {
|
||||
u.userPostQueryDo.DO = *(u.userPostQueryDo.As(alias).(*gen.DO))
|
||||
return u.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (u *userPostQuery) updateTableName(table string) *userPostQuery {
|
||||
u.ALL = field.NewAsterisk(table)
|
||||
u.ID = field.NewInt64(table, "id")
|
||||
u.CreatedAt = field.NewTime(table, "created_at")
|
||||
u.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
u.UserID = field.NewInt64(table, "user_id")
|
||||
u.PostID = field.NewInt64(table, "post_id")
|
||||
u.Price = field.NewInt64(table, "price")
|
||||
|
||||
u.fillFieldMap()
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *userPostQuery) QueryContext(ctx context.Context) (*userPostQuery, *userPostQueryDo) {
|
||||
return u, u.userPostQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (u *userPostQuery) WithContext(ctx context.Context) *userPostQueryDo {
|
||||
return u.userPostQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (u userPostQuery) TableName() string { return u.userPostQueryDo.TableName() }
|
||||
|
||||
func (u userPostQuery) Alias() string { return u.userPostQueryDo.Alias() }
|
||||
|
||||
func (u userPostQuery) Columns(cols ...field.Expr) gen.Columns {
|
||||
return u.userPostQueryDo.Columns(cols...)
|
||||
}
|
||||
|
||||
func (u *userPostQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := u.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (u *userPostQuery) fillFieldMap() {
|
||||
u.fieldMap = make(map[string]field.Expr, 6)
|
||||
u.fieldMap["id"] = u.ID
|
||||
u.fieldMap["created_at"] = u.CreatedAt
|
||||
u.fieldMap["updated_at"] = u.UpdatedAt
|
||||
u.fieldMap["user_id"] = u.UserID
|
||||
u.fieldMap["post_id"] = u.PostID
|
||||
u.fieldMap["price"] = u.Price
|
||||
}
|
||||
|
||||
func (u userPostQuery) clone(db *gorm.DB) userPostQuery {
|
||||
u.userPostQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return u
|
||||
}
|
||||
|
||||
func (u userPostQuery) replaceDB(db *gorm.DB) userPostQuery {
|
||||
u.userPostQueryDo.ReplaceDB(db)
|
||||
return u
|
||||
}
|
||||
|
||||
type userPostQueryDo struct{ gen.DO }
|
||||
|
||||
func (u userPostQueryDo) Debug() *userPostQueryDo {
|
||||
return u.withDO(u.DO.Debug())
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) WithContext(ctx context.Context) *userPostQueryDo {
|
||||
return u.withDO(u.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) ReadDB() *userPostQueryDo {
|
||||
return u.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) WriteDB() *userPostQueryDo {
|
||||
return u.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Session(config *gorm.Session) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Session(config))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Clauses(conds ...clause.Expression) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Returning(value interface{}, columns ...string) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Not(conds ...gen.Condition) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Or(conds ...gen.Condition) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Select(conds ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Where(conds ...gen.Condition) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Order(conds ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Distinct(cols ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Omit(cols ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Join(table schema.Tabler, on ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) LeftJoin(table schema.Tabler, on ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) RightJoin(table schema.Tabler, on ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Group(cols ...field.Expr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Having(conds ...gen.Condition) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Limit(limit int) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Offset(offset int) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Unscoped() *userPostQueryDo {
|
||||
return u.withDO(u.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Create(values ...*UserPost) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return u.DO.Create(values)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) CreateInBatches(values []*UserPost, batchSize int) error {
|
||||
return u.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (u userPostQueryDo) Save(values ...*UserPost) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return u.DO.Save(values)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) First() (*UserPost, error) {
|
||||
if result, err := u.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*UserPost), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Take() (*UserPost, error) {
|
||||
if result, err := u.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*UserPost), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Last() (*UserPost, error) {
|
||||
if result, err := u.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*UserPost), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Find() ([]*UserPost, error) {
|
||||
result, err := u.DO.Find()
|
||||
return result.([]*UserPost), err
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*UserPost, err error) {
|
||||
buf := make([]*UserPost, 0, batchSize)
|
||||
err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) FindInBatches(result *[]*UserPost, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return u.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Attrs(attrs ...field.AssignExpr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Assign(attrs ...field.AssignExpr) *userPostQueryDo {
|
||||
return u.withDO(u.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Joins(fields ...field.RelationField) *userPostQueryDo {
|
||||
for _, _f := range fields {
|
||||
u = *u.withDO(u.DO.Joins(_f))
|
||||
}
|
||||
return &u
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Preload(fields ...field.RelationField) *userPostQueryDo {
|
||||
for _, _f := range fields {
|
||||
u = *u.withDO(u.DO.Preload(_f))
|
||||
}
|
||||
return &u
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) FirstOrInit() (*UserPost, error) {
|
||||
if result, err := u.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*UserPost), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) FirstOrCreate() (*UserPost, error) {
|
||||
if result, err := u.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*UserPost), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) FindByPage(offset int, limit int) (result []*UserPost, count int64, err error) {
|
||||
result, err = u.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = u.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = u.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = u.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Scan(result interface{}) (err error) {
|
||||
return u.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (u userPostQueryDo) Delete(models ...*UserPost) (result gen.ResultInfo, err error) {
|
||||
return u.DO.Delete(models)
|
||||
}
|
||||
|
||||
// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
|
||||
func (u userPostQueryDo) ForceDelete() (gen.ResultInfo, error) {
|
||||
return u.Unscoped().Delete()
|
||||
}
|
||||
|
||||
// Inc increases the given column by step for current scope.
|
||||
func (u userPostQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column + step
|
||||
e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
|
||||
return u.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Dec decreases the given column by step for current scope.
|
||||
func (u userPostQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column - step
|
||||
e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
|
||||
return u.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Sum returns SUM(column) for current scope.
|
||||
func (u userPostQueryDo) Sum(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Avg returns AVG(column) for current scope.
|
||||
func (u userPostQueryDo) Avg(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Min returns MIN(column) for current scope.
|
||||
func (u userPostQueryDo) Min(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Max returns MAX(column) for current scope.
|
||||
func (u userPostQueryDo) Max(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// PluckMap returns a map[key]value for selected key/value expressions within current scope.
|
||||
func (u userPostQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
|
||||
do := u.Select(key, val)
|
||||
rows, err := do.DO.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
mm := make(map[interface{}]interface{})
|
||||
for rows.Next() {
|
||||
var k interface{}
|
||||
var v interface{}
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
return mm, rows.Err()
|
||||
}
|
||||
|
||||
// Exists returns true if any record matches the given conditions.
|
||||
func (u userPostQueryDo) Exists(conds ...gen.Condition) (bool, error) {
|
||||
cnt, err := u.Where(conds...).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// PluckIDs returns all primary key values under current scope.
|
||||
func (u userPostQueryDo) PluckIDs() ([]int64, error) {
|
||||
ids := make([]int64, 0, 16)
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
if err := u.DO.Pluck(pk, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetByID finds a single record by primary key.
|
||||
func (u userPostQueryDo) GetByID(id int64) (*UserPost, error) {
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.Eq(id)).First()
|
||||
}
|
||||
|
||||
// GetByIDs finds records by primary key list.
|
||||
func (u userPostQueryDo) GetByIDs(ids ...int64) ([]*UserPost, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*UserPost{}, nil
|
||||
}
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.In(ids...)).Find()
|
||||
}
|
||||
|
||||
// DeleteByID deletes records by primary key.
|
||||
func (u userPostQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.Eq(id)).Delete()
|
||||
}
|
||||
|
||||
// DeleteByIDs deletes records by a list of primary keys.
|
||||
func (u userPostQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
|
||||
if len(ids) == 0 {
|
||||
return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
|
||||
}
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.In(ids...)).Delete()
|
||||
}
|
||||
|
||||
func (u *userPostQueryDo) withDO(do gen.Dao) *userPostQueryDo {
|
||||
u.DO = *do.(*gen.DO)
|
||||
return u
|
||||
}
|
||||
68
backend_v1/database/models/users.gen.go
Normal file
68
backend_v1/database/models/users.gen.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const TableNameUser = "users"
|
||||
|
||||
// User mapped from table <users>
|
||||
type User struct {
|
||||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true" json:"id"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp without time zone;not null;default:now()" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp without time zone;not null;default:now()" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:timestamp without time zone" json:"deleted_at"`
|
||||
Status int16 `gorm:"column:status;type:smallint;not null" json:"status"`
|
||||
OpenID string `gorm:"column:open_id;type:character varying(128);not null" json:"open_id"`
|
||||
Username string `gorm:"column:username;type:character varying(128);not null" json:"username"`
|
||||
Avatar string `gorm:"column:avatar;type:text" json:"avatar"`
|
||||
Metas types.JSON `gorm:"column:metas;type:jsonb;not null;default:{}" json:"metas"`
|
||||
AuthToken types.JSON `gorm:"column:auth_token;type:jsonb;not null;default:{}" json:"auth_token"`
|
||||
Balance int64 `gorm:"column:balance;type:bigint;not null" json:"balance"`
|
||||
}
|
||||
|
||||
// Quick operations without importing query package
|
||||
// Update applies changed fields to the database using the default DB.
|
||||
func (m *User) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.User.WithContext(ctx).Updates(m)
|
||||
}
|
||||
|
||||
// Save upserts the model using the default DB.
|
||||
func (m *User) Save(ctx context.Context) error { return Q.User.WithContext(ctx).Save(m) }
|
||||
|
||||
// Create inserts the model using the default DB.
|
||||
func (m *User) Create(ctx context.Context) error { return Q.User.WithContext(ctx).Create(m) }
|
||||
|
||||
// Delete removes the row represented by the model using the default DB.
|
||||
func (m *User) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.User.WithContext(ctx).Delete(m)
|
||||
}
|
||||
|
||||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||||
func (m *User) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.User.WithContext(ctx).Unscoped().Delete(m)
|
||||
}
|
||||
|
||||
// Restore sets deleted_at to NULL for this model's primary key using the default DB.
|
||||
func (m *User) Restore(ctx context.Context) (gen.ResultInfo, error) {
|
||||
return Q.User.WithContext(ctx).RestoreByID(m.ID)
|
||||
}
|
||||
|
||||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||||
func (m *User) Reload(ctx context.Context) error {
|
||||
fresh, err := Q.User.WithContext(ctx).GetByID(m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*m = *fresh
|
||||
return nil
|
||||
}
|
||||
512
backend_v1/database/models/users.query.gen.go
Normal file
512
backend_v1/database/models/users.query.gen.go
Normal file
@@ -0,0 +1,512 @@
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/schema"
|
||||
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
|
||||
"gorm.io/plugin/dbresolver"
|
||||
)
|
||||
|
||||
func newUser(db *gorm.DB, opts ...gen.DOOption) userQuery {
|
||||
_userQuery := userQuery{}
|
||||
|
||||
_userQuery.userQueryDo.UseDB(db, opts...)
|
||||
_userQuery.userQueryDo.UseModel(&User{})
|
||||
|
||||
tableName := _userQuery.userQueryDo.TableName()
|
||||
_userQuery.ALL = field.NewAsterisk(tableName)
|
||||
_userQuery.ID = field.NewInt64(tableName, "id")
|
||||
_userQuery.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_userQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_userQuery.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
_userQuery.Status = field.NewInt16(tableName, "status")
|
||||
_userQuery.OpenID = field.NewString(tableName, "open_id")
|
||||
_userQuery.Username = field.NewString(tableName, "username")
|
||||
_userQuery.Avatar = field.NewString(tableName, "avatar")
|
||||
_userQuery.Metas = field.NewJSONB(tableName, "metas")
|
||||
_userQuery.AuthToken = field.NewJSONB(tableName, "auth_token")
|
||||
_userQuery.Balance = field.NewInt64(tableName, "balance")
|
||||
|
||||
_userQuery.fillFieldMap()
|
||||
|
||||
return _userQuery
|
||||
}
|
||||
|
||||
type userQuery struct {
|
||||
userQueryDo userQueryDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
Status field.Int16
|
||||
OpenID field.String
|
||||
Username field.String
|
||||
Avatar field.String
|
||||
Metas field.JSONB
|
||||
AuthToken field.JSONB
|
||||
Balance field.Int64
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
func (u userQuery) Table(newTableName string) *userQuery {
|
||||
u.userQueryDo.UseTable(newTableName)
|
||||
return u.updateTableName(newTableName)
|
||||
}
|
||||
|
||||
func (u userQuery) As(alias string) *userQuery {
|
||||
u.userQueryDo.DO = *(u.userQueryDo.As(alias).(*gen.DO))
|
||||
return u.updateTableName(alias)
|
||||
}
|
||||
|
||||
func (u *userQuery) updateTableName(table string) *userQuery {
|
||||
u.ALL = field.NewAsterisk(table)
|
||||
u.ID = field.NewInt64(table, "id")
|
||||
u.CreatedAt = field.NewTime(table, "created_at")
|
||||
u.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
u.DeletedAt = field.NewField(table, "deleted_at")
|
||||
u.Status = field.NewInt16(table, "status")
|
||||
u.OpenID = field.NewString(table, "open_id")
|
||||
u.Username = field.NewString(table, "username")
|
||||
u.Avatar = field.NewString(table, "avatar")
|
||||
u.Metas = field.NewJSONB(table, "metas")
|
||||
u.AuthToken = field.NewJSONB(table, "auth_token")
|
||||
u.Balance = field.NewInt64(table, "balance")
|
||||
|
||||
u.fillFieldMap()
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *userQuery) QueryContext(ctx context.Context) (*userQuery, *userQueryDo) {
|
||||
return u, u.userQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (u *userQuery) WithContext(ctx context.Context) *userQueryDo {
|
||||
return u.userQueryDo.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (u userQuery) TableName() string { return u.userQueryDo.TableName() }
|
||||
|
||||
func (u userQuery) Alias() string { return u.userQueryDo.Alias() }
|
||||
|
||||
func (u userQuery) Columns(cols ...field.Expr) gen.Columns { return u.userQueryDo.Columns(cols...) }
|
||||
|
||||
func (u *userQuery) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
_f, ok := u.fieldMap[fieldName]
|
||||
if !ok || _f == nil {
|
||||
return nil, false
|
||||
}
|
||||
_oe, ok := _f.(field.OrderExpr)
|
||||
return _oe, ok
|
||||
}
|
||||
|
||||
func (u *userQuery) fillFieldMap() {
|
||||
u.fieldMap = make(map[string]field.Expr, 11)
|
||||
u.fieldMap["id"] = u.ID
|
||||
u.fieldMap["created_at"] = u.CreatedAt
|
||||
u.fieldMap["updated_at"] = u.UpdatedAt
|
||||
u.fieldMap["deleted_at"] = u.DeletedAt
|
||||
u.fieldMap["status"] = u.Status
|
||||
u.fieldMap["open_id"] = u.OpenID
|
||||
u.fieldMap["username"] = u.Username
|
||||
u.fieldMap["avatar"] = u.Avatar
|
||||
u.fieldMap["metas"] = u.Metas
|
||||
u.fieldMap["auth_token"] = u.AuthToken
|
||||
u.fieldMap["balance"] = u.Balance
|
||||
}
|
||||
|
||||
func (u userQuery) clone(db *gorm.DB) userQuery {
|
||||
u.userQueryDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
return u
|
||||
}
|
||||
|
||||
func (u userQuery) replaceDB(db *gorm.DB) userQuery {
|
||||
u.userQueryDo.ReplaceDB(db)
|
||||
return u
|
||||
}
|
||||
|
||||
type userQueryDo struct{ gen.DO }
|
||||
|
||||
func (u userQueryDo) Debug() *userQueryDo {
|
||||
return u.withDO(u.DO.Debug())
|
||||
}
|
||||
|
||||
func (u userQueryDo) WithContext(ctx context.Context) *userQueryDo {
|
||||
return u.withDO(u.DO.WithContext(ctx))
|
||||
}
|
||||
|
||||
func (u userQueryDo) ReadDB() *userQueryDo {
|
||||
return u.Clauses(dbresolver.Read)
|
||||
}
|
||||
|
||||
func (u userQueryDo) WriteDB() *userQueryDo {
|
||||
return u.Clauses(dbresolver.Write)
|
||||
}
|
||||
|
||||
func (u userQueryDo) Session(config *gorm.Session) *userQueryDo {
|
||||
return u.withDO(u.DO.Session(config))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Clauses(conds ...clause.Expression) *userQueryDo {
|
||||
return u.withDO(u.DO.Clauses(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Returning(value interface{}, columns ...string) *userQueryDo {
|
||||
return u.withDO(u.DO.Returning(value, columns...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Not(conds ...gen.Condition) *userQueryDo {
|
||||
return u.withDO(u.DO.Not(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Or(conds ...gen.Condition) *userQueryDo {
|
||||
return u.withDO(u.DO.Or(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Select(conds ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Select(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Where(conds ...gen.Condition) *userQueryDo {
|
||||
return u.withDO(u.DO.Where(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Order(conds ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Order(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Distinct(cols ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Distinct(cols...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Omit(cols ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Omit(cols...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Join(table schema.Tabler, on ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Join(table, on...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) LeftJoin(table schema.Tabler, on ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.LeftJoin(table, on...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) RightJoin(table schema.Tabler, on ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.RightJoin(table, on...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Group(cols ...field.Expr) *userQueryDo {
|
||||
return u.withDO(u.DO.Group(cols...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Having(conds ...gen.Condition) *userQueryDo {
|
||||
return u.withDO(u.DO.Having(conds...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Limit(limit int) *userQueryDo {
|
||||
return u.withDO(u.DO.Limit(limit))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Offset(offset int) *userQueryDo {
|
||||
return u.withDO(u.DO.Offset(offset))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userQueryDo {
|
||||
return u.withDO(u.DO.Scopes(funcs...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Unscoped() *userQueryDo {
|
||||
return u.withDO(u.DO.Unscoped())
|
||||
}
|
||||
|
||||
func (u userQueryDo) Create(values ...*User) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return u.DO.Create(values)
|
||||
}
|
||||
|
||||
func (u userQueryDo) CreateInBatches(values []*User, batchSize int) error {
|
||||
return u.DO.CreateInBatches(values, batchSize)
|
||||
}
|
||||
|
||||
// Save : !!! underlying implementation is different with GORM
|
||||
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||
func (u userQueryDo) Save(values ...*User) error {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
return u.DO.Save(values)
|
||||
}
|
||||
|
||||
func (u userQueryDo) First() (*User, error) {
|
||||
if result, err := u.DO.First(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userQueryDo) Take() (*User, error) {
|
||||
if result, err := u.DO.Take(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userQueryDo) Last() (*User, error) {
|
||||
if result, err := u.DO.Last(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userQueryDo) Find() ([]*User, error) {
|
||||
result, err := u.DO.Find()
|
||||
return result.([]*User), err
|
||||
}
|
||||
|
||||
func (u userQueryDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*User, err error) {
|
||||
buf := make([]*User, 0, batchSize)
|
||||
err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||
defer func() { results = append(results, buf...) }()
|
||||
return fc(tx, batch)
|
||||
})
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (u userQueryDo) FindInBatches(result *[]*User, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||
return u.DO.FindInBatches(result, batchSize, fc)
|
||||
}
|
||||
|
||||
func (u userQueryDo) Attrs(attrs ...field.AssignExpr) *userQueryDo {
|
||||
return u.withDO(u.DO.Attrs(attrs...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Assign(attrs ...field.AssignExpr) *userQueryDo {
|
||||
return u.withDO(u.DO.Assign(attrs...))
|
||||
}
|
||||
|
||||
func (u userQueryDo) Joins(fields ...field.RelationField) *userQueryDo {
|
||||
for _, _f := range fields {
|
||||
u = *u.withDO(u.DO.Joins(_f))
|
||||
}
|
||||
return &u
|
||||
}
|
||||
|
||||
func (u userQueryDo) Preload(fields ...field.RelationField) *userQueryDo {
|
||||
for _, _f := range fields {
|
||||
u = *u.withDO(u.DO.Preload(_f))
|
||||
}
|
||||
return &u
|
||||
}
|
||||
|
||||
func (u userQueryDo) FirstOrInit() (*User, error) {
|
||||
if result, err := u.DO.FirstOrInit(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userQueryDo) FirstOrCreate() (*User, error) {
|
||||
if result, err := u.DO.FirstOrCreate(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.(*User), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (u userQueryDo) FindByPage(offset int, limit int) (result []*User, count int64, err error) {
|
||||
result, err = u.Offset(offset).Limit(limit).Find()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||
count = int64(size + offset)
|
||||
return
|
||||
}
|
||||
|
||||
count, err = u.Offset(-1).Limit(-1).Count()
|
||||
return
|
||||
}
|
||||
|
||||
func (u userQueryDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||
count, err = u.Count()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = u.Offset(offset).Limit(limit).Scan(result)
|
||||
return
|
||||
}
|
||||
|
||||
func (u userQueryDo) Scan(result interface{}) (err error) {
|
||||
return u.DO.Scan(result)
|
||||
}
|
||||
|
||||
func (u userQueryDo) Delete(models ...*User) (result gen.ResultInfo, err error) {
|
||||
return u.DO.Delete(models)
|
||||
}
|
||||
|
||||
// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
|
||||
func (u userQueryDo) ForceDelete() (gen.ResultInfo, error) {
|
||||
return u.Unscoped().Delete()
|
||||
}
|
||||
|
||||
// Inc increases the given column by step for current scope.
|
||||
func (u userQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column + step
|
||||
e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
|
||||
return u.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Dec decreases the given column by step for current scope.
|
||||
func (u userQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
|
||||
// column = column - step
|
||||
e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
|
||||
return u.DO.UpdateColumn(column, e)
|
||||
}
|
||||
|
||||
// Sum returns SUM(column) for current scope.
|
||||
func (u userQueryDo) Sum(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Avg returns AVG(column) for current scope.
|
||||
func (u userQueryDo) Avg(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Min returns MIN(column) for current scope.
|
||||
func (u userQueryDo) Min(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// Max returns MAX(column) for current scope.
|
||||
func (u userQueryDo) Max(column field.Expr) (float64, error) {
|
||||
var _v float64
|
||||
agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
|
||||
if err := u.Select(agg).Scan(&_v); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return _v, nil
|
||||
}
|
||||
|
||||
// PluckMap returns a map[key]value for selected key/value expressions within current scope.
|
||||
func (u userQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
|
||||
do := u.Select(key, val)
|
||||
rows, err := do.DO.Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
mm := make(map[interface{}]interface{})
|
||||
for rows.Next() {
|
||||
var k interface{}
|
||||
var v interface{}
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mm[k] = v
|
||||
}
|
||||
return mm, rows.Err()
|
||||
}
|
||||
|
||||
// Exists returns true if any record matches the given conditions.
|
||||
func (u userQueryDo) Exists(conds ...gen.Condition) (bool, error) {
|
||||
cnt, err := u.Where(conds...).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return cnt > 0, nil
|
||||
}
|
||||
|
||||
// PluckIDs returns all primary key values under current scope.
|
||||
func (u userQueryDo) PluckIDs() ([]int64, error) {
|
||||
ids := make([]int64, 0, 16)
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
if err := u.DO.Pluck(pk, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// GetByID finds a single record by primary key.
|
||||
func (u userQueryDo) GetByID(id int64) (*User, error) {
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.Eq(id)).First()
|
||||
}
|
||||
|
||||
// GetByIDs finds records by primary key list.
|
||||
func (u userQueryDo) GetByIDs(ids ...int64) ([]*User, error) {
|
||||
if len(ids) == 0 {
|
||||
return []*User{}, nil
|
||||
}
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.In(ids...)).Find()
|
||||
}
|
||||
|
||||
// DeleteByID deletes records by primary key.
|
||||
func (u userQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.Eq(id)).Delete()
|
||||
}
|
||||
|
||||
// DeleteByIDs deletes records by a list of primary keys.
|
||||
func (u userQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
|
||||
if len(ids) == 0 {
|
||||
return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
|
||||
}
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
return u.Where(pk.In(ids...)).Delete()
|
||||
}
|
||||
|
||||
// RestoreWhere sets deleted_at to NULL for rows matching current scope + conds.
|
||||
func (u userQueryDo) RestoreWhere(conds ...gen.Condition) (gen.ResultInfo, error) {
|
||||
col := field.NewField(u.TableName(), "deleted_at")
|
||||
return u.Unscoped().Where(conds...).UpdateColumn(col, nil)
|
||||
}
|
||||
|
||||
// RestoreByID sets deleted_at to NULL for the given primary key.
|
||||
func (u userQueryDo) RestoreByID(id int64) (gen.ResultInfo, error) {
|
||||
pk := field.NewInt64(u.TableName(), "id")
|
||||
col := field.NewField(u.TableName(), "deleted_at")
|
||||
return u.Unscoped().Where(pk.Eq(id)).UpdateColumn(col, nil)
|
||||
}
|
||||
|
||||
func (u *userQueryDo) withDO(do gen.Dao) *userQueryDo {
|
||||
u.DO = *do.(*gen.DO)
|
||||
return u
|
||||
}
|
||||
Reference in New Issue
Block a user