Skip to content

Commit

Permalink
refactor(internal/model): 将 internal/engine 并入 internal/model
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 9, 2024
1 parent 5486118 commit 128a098
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 41 deletions.
8 changes: 4 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"database/sql"

"github.com/issue9/orm/v6/core"
"github.com/issue9/orm/v6/internal/engine"
"github.com/issue9/orm/v6/internal/model"
"github.com/issue9/orm/v6/sqlbuilder"
)
Expand Down Expand Up @@ -44,9 +43,11 @@ func NewDB(tablePrefix, dsn string, dialect Dialect) (*DB, error) {
// NOTE: 请确保用于打开 db 的 driverName 参数与 dialect.DriverName() 是相同的,
// 否则后续操作的结果是未知的。
func NewDBWithStdDB(tablePrefix string, db *sql.DB, dialect Dialect) (*DB, error) {
ms := model.NewModels(dialect)
inst := &DB{
db: db,
Engine: engine.New(db, tablePrefix, dialect),
models: ms,
Engine: ms.NewEngine(db, tablePrefix),
}

ver, err := sqlbuilder.Version(inst)
Expand All @@ -55,7 +56,6 @@ func NewDBWithStdDB(tablePrefix string, db *sql.DB, dialect Dialect) (*DB, error
}

inst.version = ver
inst.models = model.NewModels()
inst.sqlBuilder = sqlbuilder.New(inst)

return inst, nil
Expand All @@ -69,7 +69,7 @@ func (db *DB) New(tablePrefix string) *DB {
return db
}

e := engine.New(db.DB(), tablePrefix, db.Dialect())
e := db.models.NewEngine(db.DB(), tablePrefix)
return &DB{
Engine: e,
sqlBuilder: sqlbuilder.New(e),
Expand Down
15 changes: 0 additions & 15 deletions internal/engine/engine_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/model/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func BenchmarkNewModelNoCached(b *testing.B) {
a := assert.New(b, false)
ms := NewModels()
ms := NewModels(nil)
a.NotNil(ms)

for i := 0; i < b.N; i++ {
Expand All @@ -24,7 +24,7 @@ func BenchmarkNewModelNoCached(b *testing.B) {

func BenchmarkNewModelCached(b *testing.B) {
a := assert.New(b, false)
ms := NewModels()
ms := NewModels(nil)
a.NotNil(ms)

for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion internal/model/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestColumn_setNullable(t *testing.T) {
a.Error(col.setNullable([]string{"1", "2"}))
a.Error(col.setNullable([]string{"T1"}))

ms := NewModels()
ms := NewModels(nil)
a.NotNil(ms)

// 将 AI 设置为 nullable
Expand Down
14 changes: 7 additions & 7 deletions internal/engine/engine.go → internal/model/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//
// SPDX-License-Identifier: MIT

// Package engine [core.Engine] 的默认实现
package engine
package model

import (
"context"
Expand All @@ -14,8 +13,8 @@ import (
)

type coreEngine struct {
ms *Models
engine stdEngine
dialect core.Dialect
tablePrefix string
replacer *strings.Replacer
sqlLogger func(string)
Expand All @@ -31,12 +30,13 @@ type stdEngine interface {

func defaultSQLLogger(string) {}

func New(e stdEngine, tablePrefix string, d core.Dialect) core.Engine {
l, r := d.Quotes()
// NewEngine 声明实现 [core.Engine] 接口的实例
func (ms *Models) NewEngine(e stdEngine, tablePrefix string) core.Engine {
l, r := ms.dialect.Quotes()

return &coreEngine{
ms: ms,
engine: e,
dialect: d,
tablePrefix: tablePrefix,
sqlLogger: defaultSQLLogger,
replacer: strings.NewReplacer(
Expand All @@ -60,7 +60,7 @@ func (db *coreEngine) Debug(l func(string)) {
db.sqlLogger = l
}

func (db *coreEngine) Dialect() core.Dialect { return db.dialect }
func (db *coreEngine) Dialect() core.Dialect { return db.ms.dialect }

func (db *coreEngine) QueryRow(query string, args ...any) *sql.Row {
return db.QueryRowContext(context.Background(), query, args...)
Expand Down
4 changes: 2 additions & 2 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func propertyError(field, name, message string) error {
return fmt.Errorf("%s 的 %s 属性发生以下错误: %s", field, name, message)
}

// New 从一个 obj 声明一个 Model 实例
// New 从一个 obj 声明 [core.Model] 实例
//
// obj 可以是一个 struct 实例或是指针
// obj 可以是一个结构体或是指针
func (ms *Models) New(obj core.TableNamer) (*core.Model, error) {
rtype := reflect.TypeOf(obj)
for rtype.Kind() == reflect.Ptr {
Expand Down
2 changes: 1 addition & 1 deletion internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (v *viewObject) ViewAs() (string, error) {

func TestModels_New(t *testing.T) {
a := assert.New(t, false)
ms := NewModels()
ms := NewModels(nil)
a.NotNil(ms)

m, err := ms.New(&Admin{})
Expand Down
16 changes: 11 additions & 5 deletions internal/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@

package model

import "sync"
import (
"sync"

"github.com/issue9/orm/v6/core"
)

// Models 数据模型管理
type Models struct {
models *sync.Map
dialect core.Dialect
models *sync.Map
}

// NewModels 声明 [Models] 变量
func NewModels() *Models {
func NewModels(d core.Dialect) *Models {
return &Models{
models: &sync.Map{},
dialect: d,
models: &sync.Map{},
}
}

// Clear 清除所有的 Model 缓存
// Clear 清除所有的 [core.Model] 缓存
func (ms *Models) Clear() {
ms.models.Range(func(key, _ any) bool {
ms.models.Delete(key)
Expand Down
2 changes: 1 addition & 1 deletion internal/model/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (ms *Models) len() (cnt int) {
func TestModels(t *testing.T) {
a := assert.New(t, false)

ms := NewModels()
ms := NewModels(nil)
a.NotNil(ms)

m, err := ms.New(&User{})
Expand Down
5 changes: 2 additions & 3 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"

"github.com/issue9/orm/v6/core"
"github.com/issue9/orm/v6/internal/engine"
"github.com/issue9/orm/v6/sqlbuilder"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
return &Tx{
tx: tx,
db: db,
Engine: engine.New(tx, db.TablePrefix(), db.Dialect()),
Engine: db.models.NewEngine(tx, db.TablePrefix()),
}, nil
}

Expand Down Expand Up @@ -85,7 +84,7 @@ func (tx *Tx) NewEngine(tablePrefix string) Engine {
}

return &txEngine{
Engine: engine.New(tx.Tx(), tablePrefix, tx.Dialect()),
Engine: tx.db.models.NewEngine(tx.Tx(), tablePrefix),
tx: tx,
}
}
Expand Down

0 comments on commit 128a098

Please sign in to comment.