Skip to content

Commit

Permalink
gorm manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Riete committed Aug 21, 2023
0 parents commit 59212b8
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### go template template
### go gitignore template template
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
.idea
*.DS_Store
logs
sqlite.db
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/riete/gm

go 1.21.0

require gorm.io/gorm v1.25.4

require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
210 changes: 210 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package gm

import (
"context"
"database/sql"

"gorm.io/gorm"
)

type GormManager[T any] struct {
model *T
db *gorm.DB
}

func (g *GormManager[T]) Session(opts ...ManagerOption) *gorm.DB {
return g.SessionContext(context.Background(), opts...)
}

func (g *GormManager[T]) SessionContext(ctx context.Context, opts ...ManagerOption) *gorm.DB {
db := g.db.WithContext(ctx).Order("")
for _, opt := range opts {
opt(db)
}
return db
}

func (g *GormManager[T]) First(opts ...ManagerOption) (*T, error) {
return g.FirstContext(context.Background())
}

func (g *GormManager[T]) FirstContext(ctx context.Context, opts ...ManagerOption) (*T, error) {
model := new(T)
return model, g.SessionContext(ctx, opts...).First(model).Error
}

func (g *GormManager[T]) Create(model *T) (*T, error) {
return g.CreateContext(context.Background(), model)
}

func (g *GormManager[T]) CreateContext(ctx context.Context, model *T) (*T, error) {
return model, g.SessionContext(ctx).Create(model).Error
}

func (g *GormManager[T]) BatchCreate(models ...*T) ([]*T, error) {
return g.BatchCreateContext(context.Background(), models...)
}

func (g *GormManager[T]) BatchCreateContext(ctx context.Context, models ...*T) ([]*T, error) {
return models, g.SessionContext(ctx).Create(models).Error
}

func (g *GormManager[T]) FirstOrCreate(model *T, opts ...ManagerOption) (*T, error) {
return g.FirstOrCreateContext(context.Background(), model, opts...)
}

func (g *GormManager[T]) FirstOrCreateContext(ctx context.Context, model *T, opts ...ManagerOption) (*T, error) {
return model, g.SessionContext(ctx, opts...).FirstOrCreate(model).Error
}

func (g *GormManager[T]) Update(column string, value any, opts ...ManagerOption) (int64, error) {
return g.UpdateContext(context.Background(), column, value, opts...)
}

func (g *GormManager[T]) UpdateContext(ctx context.Context, column string, value any, opts ...ManagerOption) (int64, error) {
model := new(T)
result := g.SessionContext(ctx, opts...).Model(model).Update(column, value)
return result.RowsAffected, result.Error
}

func (g *GormManager[T]) Updates(update T, opts ...ManagerOption) (int64, error) {
return g.UpdatesContext(context.Background(), update, opts...)
}

func (g *GormManager[T]) UpdatesContext(ctx context.Context, update T, opts ...ManagerOption) (int64, error) {
model := new(T)
result := g.SessionContext(ctx, opts...).Model(model).Updates(update)
return result.RowsAffected, result.Error
}

func (g *GormManager[T]) UpdateColumn(column string, value any, opts ...ManagerOption) (int64, error) {
return g.UpdateColumnContext(context.Background(), column, value, opts...)
}

func (g *GormManager[T]) UpdateColumnContext(ctx context.Context, column string, value any, opts ...ManagerOption) (int64, error) {
model := new(T)
result := g.SessionContext(ctx, opts...).Model(model).UpdateColumn(column, value)
return result.RowsAffected, result.Error
}

func (g *GormManager[T]) UpdateColumns(update T, opts ...ManagerOption) (int64, error) {
return g.UpdateColumnsContext(context.Background(), update, opts...)
}

func (g *GormManager[T]) UpdateColumnsContext(ctx context.Context, update T, opts ...ManagerOption) (int64, error) {
model := new(T)
result := g.SessionContext(ctx, opts...).Model(model).UpdateColumns(update)
return result.RowsAffected, result.Error
}

func (g *GormManager[T]) Delete(opts ...ManagerOption) error {
return g.DeleteContext(context.Background(), opts...)
}

func (g *GormManager[T]) DeleteContext(ctx context.Context, opts ...ManagerOption) error {
model := new(T)
return g.SessionContext(ctx, opts...).Delete(model).Error
}

func (g *GormManager[T]) Find(opts ...ManagerOption) ([]T, error) {
return g.FindContext(context.Background(), opts...)
}

func (g *GormManager[T]) FindContext(ctx context.Context, opts ...ManagerOption) ([]T, error) {
var models []T
return models, g.SessionContext(ctx, opts...).Find(&models).Error
}

func (g *GormManager[T]) Count(opts ...ManagerOption) (int64, error) {
return g.CountContext(context.Background(), opts...)
}

func (g *GormManager[T]) CountContext(ctx context.Context, opts ...ManagerOption) (int64, error) {
var count int64
model := new(T)
return count, g.SessionContext(ctx, opts...).Model(model).Count(&count).Error
}

func (g *GormManager[T]) Transaction(f func(tx *gorm.DB) error) error {
return g.TransactionContext(context.Background(), f)
}

func (g *GormManager[T]) TransactionContext(ctx context.Context, f func(tx *gorm.DB) error) error {
return g.SessionContext(ctx).Transaction(f)
}

func (g *GormManager[T]) Scan(dst any, opts ...ManagerOption) error {
return g.ScanContext(context.Background(), dst, opts...)
}

func (g *GormManager[T]) ScanContext(ctx context.Context, dst any, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Scan(dst).Error
}

func (g *GormManager[T]) Rows(opts ...ManagerOption) (*sql.Rows, error) {
return g.RowsContext(context.Background(), opts...)
}

func (g *GormManager[T]) RowsContext(ctx context.Context, opts ...ManagerOption) (*sql.Rows, error) {
return g.SessionContext(ctx, opts...).Rows()
}

func (g *GormManager[T]) Exec(sql string, args ...any) error {
return g.ExecContext(context.Background(), sql, args...)
}

func (g *GormManager[T]) ExecContext(ctx context.Context, sql string, args ...any) error {
return g.SessionContext(ctx).Exec(sql, args...).Error
}

func (g *GormManager[T]) AssociationFind(column string, dst any, opts ...ManagerOption) error {
return g.AssociationFindContext(context.Background(), column, dst, opts...)
}

func (g *GormManager[T]) AssociationFindContext(ctx context.Context, column string, dst any, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Association(column).Find(dst)
}

func (g *GormManager[T]) AssociationAppend(column string, dst []any, opts ...ManagerOption) error {
return g.AssociationAppendContext(context.Background(), column, dst, opts...)
}

func (g *GormManager[T]) AssociationAppendContext(ctx context.Context, column string, dst []any, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Association(column).Append(dst...)
}

func (g *GormManager[T]) AssociationReplace(column string, dst []any, opts ...ManagerOption) error {
return g.AssociationReplaceContext(context.Background(), column, dst, opts...)
}

func (g *GormManager[T]) AssociationReplaceContext(ctx context.Context, column string, dst []any, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Association(column).Replace(dst...)
}

func (g *GormManager[T]) AssociationDelete(column string, dst []any, opts ...ManagerOption) error {
return g.AssociationDeleteContext(context.Background(), column, dst, opts...)
}

func (g *GormManager[T]) AssociationDeleteContext(ctx context.Context, column string, dst []any, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Association(column).Delete(dst...)
}

func (g *GormManager[T]) AssociationClear(column string, opts ...ManagerOption) error {
return g.AssociationClearContext(context.Background(), column, opts...)
}

func (g *GormManager[T]) AssociationClearContext(ctx context.Context, column string, opts ...ManagerOption) error {
return g.SessionContext(ctx, opts...).Association(column).Clear()
}

func (g *GormManager[T]) AssociationCount(column string, opts ...ManagerOption) int64 {
return g.AssociationCountContext(context.Background(), column, opts...)
}

func (g *GormManager[T]) AssociationCountContext(ctx context.Context, column string, opts ...ManagerOption) int64 {
return g.SessionContext(ctx, opts...).Association(column).Count()
}

func NewGormManager[T any](m *T, db *gorm.DB) GormManager[T] {
return GormManager[T]{model: m, db: db}
}
95 changes: 95 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gm

import (
"gorm.io/gorm"
)

type ManagerOption func(db *gorm.DB)

func WithConditions(query any, args ...any) ManagerOption {
return func(db *gorm.DB) {
db.Where(query, args...)
}
}

func WithNotConditions(query any, args ...any) ManagerOption {
return func(db *gorm.DB) {
db.Not(query, args...)
}
}

func WithOrConditions(query any, args ...any) ManagerOption {
return func(db *gorm.DB) {
db.Or(query, args...)
}
}

func WithSelect(query any, args ...any) ManagerOption {
return func(db *gorm.DB) {
db.Select(query, args...)
}
}

func WithOmit(column ...string) ManagerOption {
return func(db *gorm.DB) {
db.Omit(column...)
}
}

func WithOffset(offset int) ManagerOption {
return func(db *gorm.DB) {
db.Offset(offset)
}
}

func WithLimit(limit int) ManagerOption {
return func(db *gorm.DB) {
db.Limit(limit)
}
}

func WithOrder(order string) ManagerOption {
return func(db *gorm.DB) {
db.Order(order)
}
}

func WithGroupByHaving(group string, having ...any) ManagerOption {
return func(db *gorm.DB) {
if len(having) > 0 {
db.Group(group).Having(having[0], having[1:]...)
} else {
db.Group(group)
}
}
}

func WithDistinct(args ...any) ManagerOption {
return func(db *gorm.DB) {
db.Distinct(args...)
}
}

func WithPreload(query string, conds ...any) ManagerOption {
return func(db *gorm.DB) {
db.Preload(query, conds...)
}
}

func WithAttrs(attrs ...any) ManagerOption {
return func(db *gorm.DB) {
db.Attrs(attrs...)
}
}

func WithAssign(attrs ...any) ManagerOption {
return func(db *gorm.DB) {
db.Assign(attrs...)
}
}

func WithRawSql(sql string) ManagerOption {
return func(db *gorm.DB) {
db.Raw(sql)
}
}

0 comments on commit 59212b8

Please sign in to comment.