Move core internal

This commit is contained in:
Lunny Xiao 2023-10-28 00:01:10 +08:00
parent 42553b7477
commit b13bff4d7c
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
27 changed files with 46 additions and 61 deletions

View File

@ -6,7 +6,7 @@ GOFMT ?= gofmt -s
TAGS ?= TAGS ?=
SED_INPLACE := sed -i SED_INPLACE := sed -i
GO_DIRS := contexts integrations core dialects internal log migrate names schemas tags GO_DIRS := contexts tests core dialects internal log migrate names schemas tags
GOFILES := $(wildcard *.go) GOFILES := $(wildcard *.go)
GOFILES += $(shell find $(GO_DIRS) -name "*.go" -type f) GOFILES += $(shell find $(GO_DIRS) -name "*.go" -type f)
INTEGRATION_PACKAGES := xorm.io/xorm/v2/tests INTEGRATION_PACKAGES := xorm.io/xorm/v2/tests
@ -59,7 +59,7 @@ build: go-check $(GO_SOURCES)
.PHONY: clean .PHONY: clean
clean: clean:
$(GO) clean -i ./... $(GO) clean -i ./...
rm -rf *.sql *.log test.db cover.out cover.html *coverage.out coverage.all integrations/*.sql rm -rf *.sql *.log test.db cover.out cover.html *coverage.out coverage.all tests/*.sql
.PHONY: coverage .PHONY: coverage
coverage: coverage:

View File

@ -14,7 +14,7 @@ import (
"strings" "strings"
"xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/convert"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/internal/utils"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -9,7 +9,7 @@ import (
"fmt" "fmt"
"time" "time"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
) )
// ScanContext represents a context when Scan // ScanContext represents a context when Scan

View File

@ -13,7 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -13,7 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -13,7 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -13,7 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -12,7 +12,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -18,8 +18,8 @@ import (
"time" "time"
"xorm.io/xorm/v2/contexts" "xorm.io/xorm/v2/contexts"
"xorm.io/xorm/v2/core"
"xorm.io/xorm/v2/dialects" "xorm.io/xorm/v2/dialects"
"xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/internal/utils"
"xorm.io/xorm/v2/log" "xorm.io/xorm/v2/log"
"xorm.io/xorm/v2/names" "xorm.io/xorm/v2/names"
@ -140,7 +140,7 @@ func (engine *Engine) BufferSize(size int) *Session {
// ShowSQL show SQL statement or not on logger if log level is great than INFO // ShowSQL show SQL statement or not on logger if log level is great than INFO
func (engine *Engine) ShowSQL(show ...bool) { func (engine *Engine) ShowSQL(show ...bool) {
engine.logger.ShowSQL(show...) engine.logger.ShowSQL(show...)
engine.DB().Logger = engine.logger engine.db.Logger = engine.logger
} }
// Logger return the logger interface // Logger return the logger interface
@ -160,7 +160,7 @@ func (engine *Engine) SetLogger(logger interface{}) {
panic("logger should implement either log.ContextLogger or log.Logger") panic("logger should implement either log.ContextLogger or log.Logger")
} }
engine.logger = realLogger engine.logger = realLogger
engine.DB().Logger = realLogger engine.db.Logger = realLogger
} }
// SetLogLevel sets the logger level // SetLogLevel sets the logger level
@ -232,22 +232,22 @@ func (engine *Engine) SQLType(c *schemas.Column) string {
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
func (engine *Engine) SetConnMaxLifetime(d time.Duration) { func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
engine.DB().SetConnMaxLifetime(d) engine.db.SetConnMaxLifetime(d)
} }
// SetConnMaxIdleTime sets the maximum amount of time a connection may be idle. // SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
func (engine *Engine) SetConnMaxIdleTime(d time.Duration) { func (engine *Engine) SetConnMaxIdleTime(d time.Duration) {
engine.DB().SetConnMaxIdleTime(d) engine.db.SetConnMaxIdleTime(d)
} }
// SetMaxOpenConns is only available for go 1.2+ // SetMaxOpenConns is only available for go 1.2+
func (engine *Engine) SetMaxOpenConns(conns int) { func (engine *Engine) SetMaxOpenConns(conns int) {
engine.DB().SetMaxOpenConns(conns) engine.db.SetMaxOpenConns(conns)
} }
// SetMaxIdleConns set the max idle connections on pool, default is 2 // SetMaxIdleConns set the max idle connections on pool, default is 2
func (engine *Engine) SetMaxIdleConns(conns int) { func (engine *Engine) SetMaxIdleConns(conns int) {
engine.DB().SetMaxIdleConns(conns) engine.db.SetMaxIdleConns(conns)
} }
// NoCascade If you do not want to auto cascade load object // NoCascade If you do not want to auto cascade load object
@ -257,16 +257,6 @@ func (engine *Engine) NoCascade() *Session {
return session.NoCascade() return session.NoCascade()
} }
// NewDB provides an interface to operate database directly
func (engine *Engine) NewDB() (*core.DB, error) {
return core.Open(engine.driverName, engine.dataSourceName)
}
// DB return the wrapper of sql.DB
func (engine *Engine) DB() *core.DB {
return engine.db
}
// Dialect return database dialect // Dialect return database dialect
func (engine *Engine) Dialect() dialects.Dialect { func (engine *Engine) Dialect() dialects.Dialect {
return engine.dialect return engine.dialect
@ -279,7 +269,7 @@ func (engine *Engine) NewSession() *Session {
// Close the engine // Close the engine
func (engine *Engine) Close() error { func (engine *Engine) Close() error {
return engine.DB().Close() return engine.db.Close()
} }
// Ping tests if database is alive // Ping tests if database is alive
@ -518,7 +508,7 @@ func (engine *Engine) dumpTables(ctx context.Context, tables []*schemas.Table, w
colNames := engine.dialect.Quoter().Join(cols, ", ") colNames := engine.dialect.Quoter().Join(cols, ", ")
destColNames := dstDialect.Quoter().Join(dstCols, ", ") destColNames := dstDialect.Quoter().Join(dstCols, ", ")
rows, err := engine.DB().QueryContext(engine.defaultContext, "SELECT "+colNames+" FROM "+engine.Quote(originalTableName)) rows, err := engine.db.QueryContext(engine.defaultContext, "SELECT "+colNames+" FROM "+engine.Quote(originalTableName))
if err != nil { if err != nil {
return err return err
} }

View File

@ -169,17 +169,17 @@ func (eg *EngineGroup) SetTagIdentifier(tagIdentifier string) {
// SetMaxIdleConns set the max idle connections on pool, default is 2 // SetMaxIdleConns set the max idle connections on pool, default is 2
func (eg *EngineGroup) SetMaxIdleConns(conns int) { func (eg *EngineGroup) SetMaxIdleConns(conns int) {
eg.Engine.DB().SetMaxIdleConns(conns) eg.Engine.db.SetMaxIdleConns(conns)
for i := 0; i < len(eg.slaves); i++ { for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].DB().SetMaxIdleConns(conns) eg.slaves[i].db.SetMaxIdleConns(conns)
} }
} }
// SetMaxOpenConns is only available for go 1.2+ // SetMaxOpenConns is only available for go 1.2+
func (eg *EngineGroup) SetMaxOpenConns(conns int) { func (eg *EngineGroup) SetMaxOpenConns(conns int) {
eg.Engine.DB().SetMaxOpenConns(conns) eg.Engine.db.SetMaxOpenConns(conns)
for i := 0; i < len(eg.slaves); i++ { for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].DB().SetMaxOpenConns(conns) eg.slaves[i].db.SetMaxOpenConns(conns)
} }
} }

View File

@ -25,7 +25,7 @@ func (h GroupPolicyHandler) Slave(eg *EngineGroup) *Engine {
// RandomPolicy implmentes randomly chose the slave of slaves // RandomPolicy implmentes randomly chose the slave of slaves
func RandomPolicy() GroupPolicyHandler { func RandomPolicy() GroupPolicyHandler {
var r = rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
return func(g *EngineGroup) *Engine { return func(g *EngineGroup) *Engine {
return g.Slaves()[r.Intn(len(g.Slaves()))] return g.Slaves()[r.Intn(len(g.Slaves()))]
} }
@ -33,16 +33,16 @@ func RandomPolicy() GroupPolicyHandler {
// WeightRandomPolicy implmentes randomly chose the slave of slaves // WeightRandomPolicy implmentes randomly chose the slave of slaves
func WeightRandomPolicy(weights []int) GroupPolicyHandler { func WeightRandomPolicy(weights []int) GroupPolicyHandler {
var rands = make([]int, 0, len(weights)) rands := make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ { for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ { for n := 0; n < weights[i]; n++ {
rands = append(rands, i) rands = append(rands, i)
} }
} }
var r = rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
return func(g *EngineGroup) *Engine { return func(g *EngineGroup) *Engine {
var slaves = g.Slaves() slaves := g.Slaves()
idx := rands[r.Intn(len(rands))] idx := rands[r.Intn(len(rands))]
if idx >= len(slaves) { if idx >= len(slaves) {
idx = len(slaves) - 1 idx = len(slaves) - 1
@ -53,10 +53,10 @@ func WeightRandomPolicy(weights []int) GroupPolicyHandler {
// RoundRobinPolicy returns a group policy handler // RoundRobinPolicy returns a group policy handler
func RoundRobinPolicy() GroupPolicyHandler { func RoundRobinPolicy() GroupPolicyHandler {
var pos = -1 pos := -1
var lock sync.Mutex var lock sync.Mutex
return func(g *EngineGroup) *Engine { return func(g *EngineGroup) *Engine {
var slaves = g.Slaves() slaves := g.Slaves()
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
@ -71,17 +71,17 @@ func RoundRobinPolicy() GroupPolicyHandler {
// WeightRoundRobinPolicy returns a group policy handler // WeightRoundRobinPolicy returns a group policy handler
func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler { func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
var rands = make([]int, 0, len(weights)) rands := make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ { for i := 0; i < len(weights); i++ {
for n := 0; n < weights[i]; n++ { for n := 0; n < weights[i]; n++ {
rands = append(rands, i) rands = append(rands, i)
} }
} }
var pos = -1 pos := -1
var lock sync.Mutex var lock sync.Mutex
return func(g *EngineGroup) *Engine { return func(g *EngineGroup) *Engine {
var slaves = g.Slaves() slaves := g.Slaves()
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
pos++ pos++
@ -100,11 +100,11 @@ func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave // LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
func LeastConnPolicy() GroupPolicyHandler { func LeastConnPolicy() GroupPolicyHandler {
return func(g *EngineGroup) *Engine { return func(g *EngineGroup) *Engine {
var slaves = g.Slaves() slaves := g.Slaves()
connections := 0 connections := 0
idx := 0 idx := 0
for i := 0; i < len(slaves); i++ { for i := 0; i < len(slaves); i++ {
openConnections := slaves[i].DB().Stats().OpenConnections openConnections := slaves[i].db.Stats().OpenConnections
if i == 0 { if i == 0 {
connections = openConnections connections = openConnections
idx = i idx = i

View File

@ -10,7 +10,7 @@ import (
"reflect" "reflect"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
) )
// Rows rows wrapper a rows to // Rows rows wrapper a rows to

View File

@ -12,8 +12,8 @@ import (
"time" "time"
"xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/convert"
"xorm.io/xorm/v2/core"
"xorm.io/xorm/v2/dialects" "xorm.io/xorm/v2/dialects"
"xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -19,7 +19,7 @@ import (
"xorm.io/xorm/v2/contexts" "xorm.io/xorm/v2/contexts"
"xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/convert"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/internal/json"
"xorm.io/xorm/v2/internal/statements" "xorm.io/xorm/v2/internal/statements"
"xorm.io/xorm/v2/log" "xorm.io/xorm/v2/log"
@ -339,11 +339,6 @@ func (session *Session) Having(conditions string) *Session {
return session return session
} }
// DB db return the wrapper of sql.DB
func (session *Session) DB() *core.DB {
return session.db()
}
func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt, err error) { func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt, err error) {
crc := crc32.ChecksumIEEE([]byte(sqlStr)) crc := crc32.ChecksumIEEE([]byte(sqlStr))
// TODO try hash(sqlStr+len(sqlStr)) // TODO try hash(sqlStr+len(sqlStr))
@ -759,7 +754,7 @@ func (session *Session) PingContext(ctx context.Context) error {
} }
session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName()) session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName())
return session.DB().PingContext(ctx) return session.db().PingContext(ctx)
} }
// disable version check // disable version check

View File

@ -13,7 +13,7 @@ import (
"time" "time"
"xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/convert"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
"xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/internal/utils"
"xorm.io/xorm/v2/schemas" "xorm.io/xorm/v2/schemas"
) )

View File

@ -8,7 +8,7 @@ import (
"database/sql" "database/sql"
"strings" "strings"
"xorm.io/xorm/v2/core" "xorm.io/xorm/v2/internal/core"
) )
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) { func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
@ -34,9 +34,9 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
if session.isAutoCommit { if session.isAutoCommit {
var db *core.DB var db *core.DB
if session.sessionType == groupSession && strings.EqualFold(strings.TrimSpace(sqlStr)[:6], "select") && !session.statement.IsForUpdate { if session.sessionType == groupSession && strings.EqualFold(strings.TrimSpace(sqlStr)[:6], "select") && !session.statement.IsForUpdate {
db = session.engine.engineGroup.Slave().DB() db = session.engine.engineGroup.Slave().db
} else { } else {
db = session.DB() db = session.db()
} }
if session.prepareStmt { if session.prepareStmt {
@ -168,14 +168,14 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
} }
if session.prepareStmt { if session.prepareStmt {
stmt, err := session.doPrepare(session.DB(), sqlStr) stmt, err := session.doPrepare(session.db(), sqlStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return stmt.ExecContext(session.ctx, args...) return stmt.ExecContext(session.ctx, args...)
} }
return session.DB().ExecContext(session.ctx, sqlStr, args...) return session.db().ExecContext(session.ctx, sqlStr, args...)
} }
// Exec raw sql // Exec raw sql

View File

@ -24,7 +24,7 @@ func (session *Session) Ping() error {
} }
session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName()) session.engine.logger.Infof("PING DATABASE %v", session.engine.DriverName())
return session.DB().PingContext(session.ctx) return session.db().PingContext(session.ctx)
} }
// CreateTable create a table according a bean // CreateTable create a table according a bean

View File

@ -7,7 +7,7 @@ package xorm
// Begin a transaction // Begin a transaction
func (session *Session) Begin() error { func (session *Session) Begin() error {
if session.isAutoCommit { if session.isAutoCommit {
tx, err := session.DB().BeginTx(session.ctx, nil) tx, err := session.db().BeginTx(session.ctx, nil)
if err != nil { if err != nil {
return err return err
} }