Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package sqlorm

import (
"fmt"
"time"
"reflect"
"time"

"github.com/tinh-tinh/tinhtinh/v2/common"
"github.com/tinh-tinh/tinhtinh/v2/core"
Expand All @@ -19,7 +19,7 @@ type OnInit func(db *gorm.DB)

type Config struct {
Dialect gorm.Dialector
Models []interface{}
Models []any
Sync bool
Options []gorm.Option
Retry *RetryOptions
Expand Down Expand Up @@ -69,7 +69,7 @@ func Inject(ref core.RefProvider) *gorm.DB {

func InjectRepository[M any](ref core.RefProvider) *Repository[M] {
var model M

ctModel := reflect.ValueOf(&model).Elem()
fnc := ctModel.MethodByName("RepositoryName")
var name string
Expand Down
33 changes: 28 additions & 5 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlorm
import (
"sync"

"github.com/tinh-tinh/tinhtinh/v2/common"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -31,7 +32,7 @@ func (repo *Repository[M]) FindAll(where Query, options ...FindOptions) ([]*M, e

var opt FindOptions
if len(options) > 0 {
opt = options[0]
opt = common.MergeStruct(options...)
}
if len(opt.Related) > 0 {
for _, key := range opt.Related {
Expand Down Expand Up @@ -87,7 +88,7 @@ func (repo *Repository[M]) FindOne(where Query, options ...FindOneOptions) (*M,

var opt FindOneOptions
if len(options) > 0 {
opt = options[0]
opt = common.MergeStruct(options...)
}
if len(opt.Related) > 0 {
for _, key := range opt.Related {
Expand Down Expand Up @@ -135,10 +136,32 @@ func (repo *Repository[M]) FindByID(id any, options ...FindOneOptions) (*M, erro
return repo.FindOne(map[string]interface{}{"id": id}, options...)
}

func (repo *Repository[M]) Count(where interface{}, args ...interface{}) (int64, error) {
func (repo *Repository[M]) Count(where interface{}, options ...FindOneOptions) (int64, error) {
var count int64
var model M
result := repo.DB.Model(&model).Where(where, args...).Count(&count)

var opt FindOneOptions
if len(options) > 0 {
opt = common.MergeStruct(options...)
}

tx := repo.DB.Model(&model)
if opt.WithDeleted {
tx = tx.Unscoped()
}

if IsQueryBuilder(where) {
queryFnc, ok := where.(func(qb *QueryBuilder))
if ok {
qb := &QueryBuilder{qb: tx}
queryFnc(qb)
tx = qb.qb
}
} else {
tx = tx.Where(where)
}

result := tx.Count(&count)
if result.Error != nil {
return 0, result.Error
}
Expand All @@ -151,7 +174,7 @@ func (repo *Repository[M]) Exist(where Query, options ...FindOneOptions) (bool,

var opt FindOneOptions
if len(options) > 0 {
opt = options[0]
opt = common.MergeStruct(options...)
}
if opt.Select != nil {
tx = tx.Select(opt.Select)
Expand Down
13 changes: 10 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func Test_Count(t *testing.T) {
require.Equal(t, int64(4), count)

// Test counting with non-existent conditions
count, err = repo.Count(map[string]interface{}{
"status": "non-existent",
"priority": 999,
count, err = repo.Count(func(q *sqlorm.QueryBuilder) {
q.Equal("status", "non-existent")
q.Equal("priority", 999)
})
require.Nil(t, err)
require.Equal(t, int64(0), count)
Expand All @@ -134,6 +134,13 @@ func Test_Count(t *testing.T) {
require.Nil(t, err)
require.Equal(t, int64(1), count)

countWithDeleted, err := repo.Count(map[string]any{
"status": "active",
}, sqlorm.FindOneOptions{
WithDeleted: true,
})
require.Nil(t, err)
require.Equal(t, int64(2), countWithDeleted)
// Reset test data by deleting all records
err = db.Unscoped().Where("1 = 1").Delete(&Count{}).Error
require.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions tenancy/tenancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ForRoot(opt Options) core.Modules {
return nil
}
if mapper[tenantID] == nil {
err := CreateDabaseIfNotExist(tenantID, connectOpt)
err := CreateDatabaseIfNotExist(tenantID, connectOpt)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func InjectRepository[M any](module core.RefProvider, ctx core.Ctx) *sqlorm.Repo
return data
}

func CreateDabaseIfNotExist(dbName string, opt ConnectOptions) error {
func CreateDatabaseIfNotExist(dbName string, opt ConnectOptions) error {
conStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=disable TimeZone=Asia/Shanghai", opt.Host, opt.Port, opt.User, opt.Password)
db, err := gorm.Open(postgres.Open(conStr), &gorm.Config{})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion tenancy/tenancy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func Test_Sync(t *testing.T) {
}

func Test_NIl(t *testing.T) {

type User struct {
sqlorm.Model `gorm:"embedded"`
Name string `gorm:"type:varchar(255);not null"`
Expand Down