Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
|
dc4d31a9fd |
2
go.mod
2
go.mod
|
@ -4,8 +4,10 @@ go 1.22.9
|
|||
|
||||
require (
|
||||
git.nobla.cn/golang/kos v0.1.32
|
||||
github.com/bwmarrin/snowflake v0.3.0
|
||||
github.com/cespare/xxhash/v2 v2.3.0
|
||||
github.com/go-playground/validator/v10 v10.23.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/longbridgeapp/sqlparser v0.3.2
|
||||
github.com/rs/xid v1.6.0
|
||||
github.com/uole/sqlparser v0.0.1
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,7 @@
|
|||
git.nobla.cn/golang/kos v0.1.32 h1:sFVCA7vKc8dPUd0cxzwExOSPX2mmMh2IuwL6cYS1pBc=
|
||||
git.nobla.cn/golang/kos v0.1.32/go.mod h1:35Z070+5oB39WcVrh5DDlnVeftL/Ccmscw2MZFe9fUg=
|
||||
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
||||
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
@ -16,6 +18,8 @@ github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL
|
|||
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
|
||||
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
|
|
2
hook.go
2
hook.go
|
@ -78,7 +78,6 @@ func (hook *hookManager) afterCreate(ctx context.Context, tx *gorm.DB, model any
|
|||
cb(ctx, tx, model, diff)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (hook *hookManager) beforeUpdate(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
||||
|
@ -106,7 +105,6 @@ func (hook *hookManager) afterUpdate(ctx context.Context, tx *gorm.DB, model any
|
|||
cb(ctx, tx, model, diff)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (hook *hookManager) beforeSave(ctx context.Context, tx *gorm.DB, model any) (err error) {
|
||||
|
|
5
model.go
5
model.go
|
@ -695,9 +695,12 @@ func (m *Model) Delete(w http.ResponseWriter, r *http.Request) {
|
|||
if errTx != nil {
|
||||
return
|
||||
}
|
||||
m.getHook().afterDelete(childCtx, tx, model)
|
||||
return
|
||||
}); err == nil {
|
||||
if deleter, ok := model.(afterDeleted); ok {
|
||||
deleter.AfterDeleted(childCtx, dbSess)
|
||||
}
|
||||
m.getHook().afterDelete(childCtx, dbSess, model)
|
||||
m.response.Success(w, types.DeleteResponse{
|
||||
ID: idStr,
|
||||
Status: "deleted",
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package identity
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/xid"
|
||||
)
|
||||
|
||||
type Engine interface {
|
||||
NextID() string
|
||||
}
|
||||
|
||||
type (
|
||||
XidEngine struct {
|
||||
}
|
||||
|
||||
UUIDEngine struct {
|
||||
}
|
||||
|
||||
SnowflakeEngine struct {
|
||||
e *snowflake.Node
|
||||
}
|
||||
)
|
||||
|
||||
func (e *XidEngine) NextID() string {
|
||||
return xid.New().String()
|
||||
}
|
||||
|
||||
func (e *UUIDEngine) NextID() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
func (e *SnowflakeEngine) NextID() string {
|
||||
return e.e.Generate().String()
|
||||
}
|
||||
|
||||
func NewXidEngine() *XidEngine {
|
||||
return &XidEngine{}
|
||||
}
|
||||
|
||||
func NewUUIDEngine() *UUIDEngine {
|
||||
return &UUIDEngine{}
|
||||
}
|
||||
|
||||
func NewSnowflakeEngine(nodeID int64) *SnowflakeEngine {
|
||||
node, err := snowflake.NewNode(nodeID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &SnowflakeEngine{e: node}
|
||||
}
|
|
@ -3,12 +3,12 @@ package identity
|
|||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/rs/xid"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type Identify struct {
|
||||
e Engine
|
||||
}
|
||||
|
||||
func (identity *Identify) Name() string {
|
||||
|
@ -21,7 +21,7 @@ func (identity *Identify) Initialize(db *gorm.DB) (err error) {
|
|||
}
|
||||
|
||||
func (identity *Identify) NextID() string {
|
||||
return xid.New().String()
|
||||
return identity.e.NextID()
|
||||
}
|
||||
|
||||
func (identity *Identify) Grant(db *gorm.DB) {
|
||||
|
@ -56,6 +56,11 @@ func (identity *Identify) Grant(db *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
func New() *Identify {
|
||||
return &Identify{}
|
||||
func New(e Engine) *Identify {
|
||||
if e == nil {
|
||||
e = NewXidEngine()
|
||||
}
|
||||
return &Identify{
|
||||
e: e,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package validate
|
||||
|
||||
import (
|
||||
"git.nobla.cn/golang/rest/types"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"git.nobla.cn/golang/rest/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -14,7 +15,7 @@ const (
|
|||
|
||||
var (
|
||||
scopeCtxKey = &validateScope{}
|
||||
telephoneRegex = regexp.MustCompile("^\\d{5,20}$")
|
||||
telephoneRegex = regexp.MustCompile(`^\d{5,20}$`)
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -62,7 +63,6 @@ func formatError(rule types.Rule, scm *types.Schema, tag string) string {
|
|||
switch tag {
|
||||
case "db_unique":
|
||||
s = scm.Label + "值已经存在."
|
||||
break
|
||||
case "required":
|
||||
s = scm.Label + "值不能为空."
|
||||
case "max":
|
||||
|
|
12
query.go
12
query.go
|
@ -17,7 +17,7 @@ type (
|
|||
db *gorm.DB
|
||||
condition string
|
||||
fields []string
|
||||
params []interface{}
|
||||
params []any
|
||||
table string
|
||||
joins []join
|
||||
orderBy []string
|
||||
|
@ -29,7 +29,7 @@ type (
|
|||
|
||||
condition struct {
|
||||
Field string `json:"field"`
|
||||
Value interface{} `json:"value"`
|
||||
Value any `json:"value"`
|
||||
Expr string `json:"expr"`
|
||||
}
|
||||
|
||||
|
@ -60,16 +60,16 @@ func (query *Query) compile() (*gorm.DB, error) {
|
|||
if query.table != "" {
|
||||
db = db.Table(query.table)
|
||||
}
|
||||
if query.joins != nil && len(query.joins) > 0 {
|
||||
if len(query.joins) > 0 {
|
||||
for _, joinEntity := range query.joins {
|
||||
cs, ps := query.buildConditions("OR", false, joinEntity.Conditions...)
|
||||
db = db.Joins(joinEntity.Direction+" JOIN "+joinEntity.Table+" ON "+cs, ps...)
|
||||
}
|
||||
}
|
||||
if query.orderBy != nil && len(query.orderBy) > 0 {
|
||||
if len(query.orderBy) > 0 {
|
||||
db = db.Order(strings.Join(query.orderBy, ","))
|
||||
}
|
||||
if query.groupBy != nil && len(query.groupBy) > 0 {
|
||||
if len(query.groupBy) > 0 {
|
||||
db = db.Group(strings.Join(query.groupBy, ","))
|
||||
}
|
||||
if query.offset > 0 {
|
||||
|
@ -366,7 +366,7 @@ func (query *Query) ResetSelect() *Query {
|
|||
return query
|
||||
}
|
||||
|
||||
func (query *Query) Count(v interface{}) (i int64) {
|
||||
func (query *Query) Count(v any) (i int64) {
|
||||
var (
|
||||
db *gorm.DB
|
||||
err error
|
||||
|
|
5
types.go
5
types.go
|
@ -62,6 +62,11 @@ type (
|
|||
AfterSaved(ctx context.Context, tx *gorm.DB)
|
||||
}
|
||||
|
||||
//删除后的回调
|
||||
afterDeleted interface {
|
||||
AfterDeleted(ctx context.Context, tx *gorm.DB)
|
||||
}
|
||||
|
||||
sqlCountResponse struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ func (n Scenarios) Value() (driver.Value, error) {
|
|||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *Rule) Scan(value interface{}) error {
|
||||
func (n *Rule) Scan(value any) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ func (n Scenarios) Has(str string) bool {
|
|||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (n *Scenarios) Scan(value interface{}) error {
|
||||
func (n *Scenarios) Scan(value any) error {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue