Move core internal
This commit is contained in:
parent
42553b7477
commit
b13bff4d7c
4
Makefile
4
Makefile
|
@ -6,7 +6,7 @@ GOFMT ?= gofmt -s
|
|||
TAGS ?=
|
||||
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 += $(shell find $(GO_DIRS) -name "*.go" -type f)
|
||||
INTEGRATION_PACKAGES := xorm.io/xorm/v2/tests
|
||||
|
@ -59,7 +59,7 @@ build: go-check $(GO_SOURCES)
|
|||
.PHONY: clean
|
||||
clean:
|
||||
$(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
|
||||
coverage:
|
||||
|
|
|
@ -14,7 +14,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"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/schemas"
|
||||
)
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
)
|
||||
|
||||
// ScanContext represents a context when Scan
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
28
engine.go
28
engine.go
|
@ -18,8 +18,8 @@ import (
|
|||
"time"
|
||||
|
||||
"xorm.io/xorm/v2/contexts"
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/dialects"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/internal/utils"
|
||||
"xorm.io/xorm/v2/log"
|
||||
"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
|
||||
func (engine *Engine) ShowSQL(show ...bool) {
|
||||
engine.logger.ShowSQL(show...)
|
||||
engine.DB().Logger = engine.logger
|
||||
engine.db.Logger = engine.logger
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
engine.logger = realLogger
|
||||
engine.DB().Logger = realLogger
|
||||
engine.db.Logger = realLogger
|
||||
}
|
||||
|
||||
// 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.
|
||||
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.
|
||||
func (engine *Engine) SetConnMaxIdleTime(d time.Duration) {
|
||||
engine.DB().SetConnMaxIdleTime(d)
|
||||
engine.db.SetConnMaxIdleTime(d)
|
||||
}
|
||||
|
||||
// SetMaxOpenConns is only available for go 1.2+
|
||||
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
|
||||
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
|
||||
|
@ -257,16 +257,6 @@ func (engine *Engine) NoCascade() *Session {
|
|||
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
|
||||
func (engine *Engine) Dialect() dialects.Dialect {
|
||||
return engine.dialect
|
||||
|
@ -279,7 +269,7 @@ func (engine *Engine) NewSession() *Session {
|
|||
|
||||
// Close the engine
|
||||
func (engine *Engine) Close() error {
|
||||
return engine.DB().Close()
|
||||
return engine.db.Close()
|
||||
}
|
||||
|
||||
// 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, ", ")
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -169,17 +169,17 @@ func (eg *EngineGroup) SetTagIdentifier(tagIdentifier string) {
|
|||
|
||||
// SetMaxIdleConns set the max idle connections on pool, default is 2
|
||||
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
|
||||
eg.Engine.DB().SetMaxIdleConns(conns)
|
||||
eg.Engine.db.SetMaxIdleConns(conns)
|
||||
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+
|
||||
func (eg *EngineGroup) SetMaxOpenConns(conns int) {
|
||||
eg.Engine.DB().SetMaxOpenConns(conns)
|
||||
eg.Engine.db.SetMaxOpenConns(conns)
|
||||
for i := 0; i < len(eg.slaves); i++ {
|
||||
eg.slaves[i].DB().SetMaxOpenConns(conns)
|
||||
eg.slaves[i].db.SetMaxOpenConns(conns)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ func (h GroupPolicyHandler) Slave(eg *EngineGroup) *Engine {
|
|||
|
||||
// RandomPolicy implmentes randomly chose the slave of slaves
|
||||
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 g.Slaves()[r.Intn(len(g.Slaves()))]
|
||||
}
|
||||
|
@ -33,16 +33,16 @@ func RandomPolicy() GroupPolicyHandler {
|
|||
|
||||
// WeightRandomPolicy implmentes randomly chose the slave of slaves
|
||||
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 n := 0; n < weights[i]; n++ {
|
||||
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 {
|
||||
var slaves = g.Slaves()
|
||||
slaves := g.Slaves()
|
||||
idx := rands[r.Intn(len(rands))]
|
||||
if idx >= len(slaves) {
|
||||
idx = len(slaves) - 1
|
||||
|
@ -53,10 +53,10 @@ func WeightRandomPolicy(weights []int) GroupPolicyHandler {
|
|||
|
||||
// RoundRobinPolicy returns a group policy handler
|
||||
func RoundRobinPolicy() GroupPolicyHandler {
|
||||
var pos = -1
|
||||
pos := -1
|
||||
var lock sync.Mutex
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
slaves := g.Slaves()
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
@ -71,17 +71,17 @@ func RoundRobinPolicy() GroupPolicyHandler {
|
|||
|
||||
// WeightRoundRobinPolicy returns a group policy handler
|
||||
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 n := 0; n < weights[i]; n++ {
|
||||
rands = append(rands, i)
|
||||
}
|
||||
}
|
||||
var pos = -1
|
||||
pos := -1
|
||||
var lock sync.Mutex
|
||||
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
slaves := g.Slaves()
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
pos++
|
||||
|
@ -100,11 +100,11 @@ func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
|
|||
// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
|
||||
func LeastConnPolicy() GroupPolicyHandler {
|
||||
return func(g *EngineGroup) *Engine {
|
||||
var slaves = g.Slaves()
|
||||
slaves := g.Slaves()
|
||||
connections := 0
|
||||
idx := 0
|
||||
for i := 0; i < len(slaves); i++ {
|
||||
openConnections := slaves[i].DB().Stats().OpenConnections
|
||||
openConnections := slaves[i].db.Stats().OpenConnections
|
||||
if i == 0 {
|
||||
connections = openConnections
|
||||
idx = i
|
||||
|
|
2
rows.go
2
rows.go
|
@ -10,7 +10,7 @@ import (
|
|||
"reflect"
|
||||
|
||||
"xorm.io/builder"
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
)
|
||||
|
||||
// Rows rows wrapper a rows to
|
||||
|
|
2
scan.go
2
scan.go
|
@ -12,8 +12,8 @@ import (
|
|||
"time"
|
||||
|
||||
"xorm.io/xorm/v2/convert"
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/dialects"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
"xorm.io/xorm/v2/schemas"
|
||||
)
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ import (
|
|||
|
||||
"xorm.io/xorm/v2/contexts"
|
||||
"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/statements"
|
||||
"xorm.io/xorm/v2/log"
|
||||
|
@ -339,11 +339,6 @@ func (session *Session) Having(conditions string) *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) {
|
||||
crc := crc32.ChecksumIEEE([]byte(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())
|
||||
return session.DB().PingContext(ctx)
|
||||
return session.db().PingContext(ctx)
|
||||
}
|
||||
|
||||
// disable version check
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"time"
|
||||
|
||||
"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/schemas"
|
||||
)
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm/v2/core"
|
||||
"xorm.io/xorm/v2/internal/core"
|
||||
)
|
||||
|
||||
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 {
|
||||
var db *core.DB
|
||||
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 {
|
||||
db = session.DB()
|
||||
db = session.db()
|
||||
}
|
||||
|
||||
if session.prepareStmt {
|
||||
|
@ -168,14 +168,14 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
|
|||
}
|
||||
|
||||
if session.prepareStmt {
|
||||
stmt, err := session.doPrepare(session.DB(), sqlStr)
|
||||
stmt, err := session.doPrepare(session.db(), sqlStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
|
|
|
@ -24,7 +24,7 @@ func (session *Session) Ping() error {
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -7,7 +7,7 @@ package xorm
|
|||
// Begin a transaction
|
||||
func (session *Session) Begin() error {
|
||||
if session.isAutoCommit {
|
||||
tx, err := session.DB().BeginTx(session.ctx, nil)
|
||||
tx, err := session.db().BeginTx(session.ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue