THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Browse Source

Add support Engine Group (#748)

* add support group engine

* revert code

* add NewGroup function

* add engine group policy

* rename file name

* modify policy interface

* remove Init function from policy interface

* refactor Group Policy

* rename and comments

* rename and bug fix for WeightRoundRobinPolicy

* modify Slave function

* modify Slave function and add LeastConnPolicy

* use original Engine and Session

* remove unused count variables

* fix bug on NewEngineGroup

* remove unused method

* improve range and refactor

* add some comments and refactor

* implement GroupPolicy of GroupPolicyHandler

* refactor

* simple code

* add tests support for EngineGroup & fix some bugs

* improve the NewEngineGroup interface

* change the default policy of engine group

* fix some tests
tags/v0.6.4
Lunny Xiao GitHub 2 years ago
parent
commit
109cb1a7d0

+ 3
- 3
cache_test.go View File

@@ -20,7 +20,7 @@ func TestCacheFind(t *testing.T) {
Password string
}

oldCacher := testEngine.Cacher
oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)

@@ -95,7 +95,7 @@ func TestCacheFind2(t *testing.T) {
Password string
}

oldCacher := testEngine.Cacher
oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)

@@ -146,7 +146,7 @@ func TestCacheGet(t *testing.T) {
Password string
}

oldCacher := testEngine.Cacher
oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
testEngine.SetDefaultCacher(cacher)


+ 58
- 15
engine.go View File

@@ -47,6 +47,23 @@ type Engine struct {
disableGlobalCache bool

tagHandlers map[string]tagHandler

engineGroup *EngineGroup
}

// BufferSize sets buffer size for iterate
func (engine *Engine) BufferSize(size int) *Session {
session := engine.NewSession()
session.isAutoClose = true
return session.BufferSize(size)
}

// CondDeleted returns the conditions whether a record is soft deleted.
func (engine *Engine) CondDeleted(colName string) builder.Cond {
if engine.dialect.DBType() == core.MSSQL {
return builder.IsNull{colName}
}
return builder.IsNull{colName}.Or(builder.Eq{colName: zeroTime1})
}

// ShowSQL show SQL statement or not on logger if log level is great than INFO
@@ -79,6 +96,11 @@ func (engine *Engine) SetLogger(logger core.ILogger) {
engine.dialect.SetLogger(logger)
}

// SetLogLevel sets the logger level
func (engine *Engine) SetLogLevel(level core.LogLevel) {
engine.logger.SetLevel(level)
}

// SetDisableGlobalCache disable global cache or not
func (engine *Engine) SetDisableGlobalCache(disable bool) {
if engine.disableGlobalCache != disable {
@@ -201,6 +223,11 @@ func (engine *Engine) SetDefaultCacher(cacher core.Cacher) {
engine.Cacher = cacher
}

// GetDefaultCacher returns the default cacher
func (engine *Engine) GetDefaultCacher() core.Cacher {
return engine.Cacher
}

// NoCache If you has set default cacher, and you want temporilly stop use cache,
// you can use NoCache()
func (engine *Engine) NoCache() *Session {
@@ -757,7 +784,8 @@ func (engine *Engine) Having(conditions string) *Session {
return session.Having(conditions)
}

func (engine *Engine) unMapType(t reflect.Type) {
// UnMapType removes the datbase mapper of a type
func (engine *Engine) UnMapType(t reflect.Type) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
delete(engine.Tables, t)
@@ -1564,24 +1592,39 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}
return
}

// Unscoped always disable struct tag "deleted"
func (engine *Engine) Unscoped() *Session {
session := engine.NewSession()
session.isAutoClose = true
return session.Unscoped()
// GetColumnMapper returns the column name mapper
func (engine *Engine) GetColumnMapper() core.IMapper {
return engine.ColumnMapper
}

// CondDeleted returns the conditions whether a record is soft deleted.
func (engine *Engine) CondDeleted(colName string) builder.Cond {
if engine.dialect.DBType() == core.MSSQL {
return builder.IsNull{colName}
}
return builder.IsNull{colName}.Or(builder.Eq{colName: zeroTime1})
// GetTableMapper returns the table name mapper
func (engine *Engine) GetTableMapper() core.IMapper {
return engine.TableMapper
}

// BufferSize sets buffer size for iterate
func (engine *Engine) BufferSize(size int) *Session {
// GetTZLocation returns time zone of the application
func (engine *Engine) GetTZLocation() *time.Location {
return engine.TZLocation
}

// SetTZLocation sets time zone of the application
func (engine *Engine) SetTZLocation(tz *time.Location) {
engine.TZLocation = tz
}

// GetTZDatabase returns time zone of the database
func (engine *Engine) GetTZDatabase() *time.Location {
return engine.DatabaseTZ
}

// SetTZDatabase sets time zone of the database
func (engine *Engine) SetTZDatabase(tz *time.Location) {
engine.DatabaseTZ = tz
}

// Unscoped always disable struct tag "deleted"
func (engine *Engine) Unscoped() *Session {
session := engine.NewSession()
session.isAutoClose = true
return session.BufferSize(size)
return session.Unscoped()
}

+ 194
- 0
engine_group.go View File

@@ -0,0 +1,194 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"github.com/go-xorm/core"
)

// EngineGroup defines an engine group
type EngineGroup struct {
*Engine
slaves []*Engine
policy GroupPolicy
}

// NewEngineGroup creates a new engine group
func NewEngineGroup(args1 interface{}, args2 interface{}, policies ...GroupPolicy) (*EngineGroup, error) {
var eg EngineGroup
if len(policies) > 0 {
eg.policy = policies[0]
} else {
eg.policy = RoundRobinPolicy()
}

driverName, ok1 := args1.(string)
conns, ok2 := args2.([]string)
if ok1 && ok2 {
engines := make([]*Engine, len(conns))
for i, conn := range conns {
engine, err := NewEngine(driverName, conn)
if err != nil {
return nil, err
}
engine.engineGroup = &eg
engines[i] = engine
}

eg.Engine = engines[0]
eg.slaves = engines[1:]
return &eg, nil
}

master, ok3 := args1.(*Engine)
slaves, ok4 := args2.([]*Engine)
if ok3 && ok4 {
master.engineGroup = &eg
for i := 0; i < len(slaves); i++ {
slaves[i].engineGroup = &eg
}
eg.Engine = master
eg.slaves = slaves
return &eg, nil
}
return nil, ErrParamsType
}

// Close the engine
func (eg *EngineGroup) Close() error {
err := eg.Engine.Close()
if err != nil {
return err
}

for i := 0; i < len(eg.slaves); i++ {
err := eg.slaves[i].Close()
if err != nil {
return err
}
}
return nil
}

// Master returns the master engine
func (eg *EngineGroup) Master() *Engine {
return eg.Engine
}

// Ping tests if database is alive
func (eg *EngineGroup) Ping() error {
if err := eg.Engine.Ping(); err != nil {
return err
}

for _, slave := range eg.slaves {
if err := slave.Ping(); err != nil {
return err
}
}
return nil
}

// SetColumnMapper set the column name mapping rule
func (eg *EngineGroup) SetColumnMapper(mapper core.IMapper) {
eg.Engine.ColumnMapper = mapper
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].ColumnMapper = mapper
}
}

// SetDefaultCacher set the default cacher
func (eg *EngineGroup) SetDefaultCacher(cacher core.Cacher) {
eg.Engine.SetDefaultCacher(cacher)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetDefaultCacher(cacher)
}
}

// SetLogger set the new logger
func (eg *EngineGroup) SetLogger(logger core.ILogger) {
eg.Engine.SetLogger(logger)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetLogger(logger)
}
}

// SetLogLevel sets the logger level
func (eg *EngineGroup) SetLogLevel(level core.LogLevel) {
eg.Engine.SetLogLevel(level)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetLogLevel(level)
}
}

// SetMapper set the name mapping rules
func (eg *EngineGroup) SetMapper(mapper core.IMapper) {
eg.Engine.SetMapper(mapper)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetMapper(mapper)
}
}

// SetMaxIdleConns set the max idle connections on pool, default is 2
func (eg *EngineGroup) SetMaxIdleConns(conns int) {
eg.Engine.db.SetMaxIdleConns(conns)
for i := 0; i < len(eg.slaves); i++ {
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)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].db.SetMaxOpenConns(conns)
}
}

// SetPolicy set the group policy
func (eg *EngineGroup) SetPolicy(policy GroupPolicy) *EngineGroup {
eg.policy = policy
return eg
}

// SetTableMapper set the table name mapping rule
func (eg *EngineGroup) SetTableMapper(mapper core.IMapper) {
eg.Engine.TableMapper = mapper
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].TableMapper = mapper
}
}

// ShowExecTime show SQL statement and execute time or not on logger if log level is great than INFO
func (eg *EngineGroup) ShowExecTime(show ...bool) {
eg.Engine.ShowExecTime(show...)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].ShowExecTime(show...)
}
}

// ShowSQL show SQL statement or not on logger if log level is great than INFO
func (eg *EngineGroup) ShowSQL(show ...bool) {
eg.Engine.ShowSQL(show...)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].ShowSQL(show...)
}
}

// Slave returns one of the physical databases which is a slave according the policy
func (eg *EngineGroup) Slave() *Engine {
switch len(eg.slaves) {
case 0:
return eg.Engine
case 1:
return eg.slaves[0]
}
return eg.policy.Slave(eg)
}

// Slaves returns all the slaves
func (eg *EngineGroup) Slaves() []*Engine {
return eg.slaves
}

+ 116
- 0
engine_group_policy.go View File

@@ -0,0 +1,116 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"math/rand"
"sync"
"time"
)

// GroupPolicy is be used by chosing the current slave from slaves
type GroupPolicy interface {
Slave(*EngineGroup) *Engine
}

// GroupPolicyHandler should be used when a function is a GroupPolicy
type GroupPolicyHandler func(*EngineGroup) *Engine

// Slave implements the chosen of slaves
func (h GroupPolicyHandler) Slave(eg *EngineGroup) *Engine {
return h(eg)
}

// RandomPolicy implmentes randomly chose the slave of slaves
func RandomPolicy() GroupPolicyHandler {
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
return func(g *EngineGroup) *Engine {
return g.Slaves()[r.Intn(len(g.Slaves()))]
}
}

// WeightRandomPolicy implmentes randomly chose the slave of slaves
func WeightRandomPolicy(weights []int) GroupPolicyHandler {
var 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()))

return func(g *EngineGroup) *Engine {
var slaves = g.Slaves()
idx := rands[r.Intn(len(rands))]
if idx >= len(slaves) {
idx = len(slaves) - 1
}
return slaves[idx]
}
}

func RoundRobinPolicy() GroupPolicyHandler {
var pos = -1
var lock sync.Mutex
return func(g *EngineGroup) *Engine {
var slaves = g.Slaves()

lock.Lock()
defer lock.Unlock()
pos++
if pos >= len(slaves) {
pos = 0
}

return slaves[pos]
}
}

func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
var 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
var lock sync.Mutex

return func(g *EngineGroup) *Engine {
var slaves = g.Slaves()
lock.Lock()
defer lock.Unlock()
pos++
if pos >= len(rands) {
pos = 0
}

idx := rands[pos]
if idx >= len(slaves) {
idx = len(slaves) - 1
}
return slaves[idx]
}
}

// LeastConnPolicy implements GroupPolicy, every time will get the least connections slave
func LeastConnPolicy() GroupPolicyHandler {
return func(g *EngineGroup) *Engine {
var slaves = g.Slaves()
connections := 0
idx := 0
for i := 0; i < len(slaves); i++ {
openConnections := slaves[i].DB().Stats().OpenConnections
if i == 0 {
connections = openConnections
idx = i
} else if openConnections <= connections {
connections = openConnections
idx = i
}
}
return slaves[idx]
}
}

+ 8
- 0
engine_maxlife.go View File

@@ -12,3 +12,11 @@ import "time"
func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
engine.db.SetConnMaxLifetime(d)
}

// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
func (eg *EngineGroup) SetConnMaxLifetime(d time.Duration) {
eg.Engine.SetConnMaxLifetime(d)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetConnMaxLifetime(d)
}
}

+ 103
- 0
interface.go View File

@@ -0,0 +1,103 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"database/sql"
"reflect"
"time"

"github.com/go-xorm/core"
)

// Interface defines the interface which Engine, EngineGroup and Session will implementate.
type Interface interface {
AllCols() *Session
Alias(alias string) *Session
Asc(colNames ...string) *Session
BufferSize(size int) *Session
Cols(columns ...string) *Session
Count(...interface{}) (int64, error)
CreateIndexes(bean interface{}) error
CreateUniques(bean interface{}) error
Decr(column string, arg ...interface{}) *Session
Desc(...string) *Session
Delete(interface{}) (int64, error)
Distinct(columns ...string) *Session
DropIndexes(bean interface{}) error
Exec(string, ...interface{}) (sql.Result, error)
Exist(bean ...interface{}) (bool, error)
Find(interface{}, ...interface{}) error
Get(interface{}) (bool, error)
GroupBy(keys string) *Session
ID(interface{}) *Session
In(string, ...interface{}) *Session
Incr(column string, arg ...interface{}) *Session
Insert(...interface{}) (int64, error)
InsertOne(interface{}) (int64, error)
IsTableEmpty(bean interface{}) (bool, error)
IsTableExist(beanOrTableName interface{}) (bool, error)
Iterate(interface{}, IterFunc) error
Limit(int, ...int) *Session
NoAutoCondition(...bool) *Session
NotIn(string, ...interface{}) *Session
Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session
Omit(columns ...string) *Session
OrderBy(order string) *Session
Ping() error
Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error)
QueryInterface(sqlStr string, args ...interface{}) ([]map[string]interface{}, error)
QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error)
Rows(bean interface{}) (*Rows, error)
SetExpr(string, string) *Session
SQL(interface{}, ...interface{}) *Session
Sum(bean interface{}, colName string) (float64, error)
SumInt(bean interface{}, colName string) (int64, error)
Sums(bean interface{}, colNames ...string) ([]float64, error)
SumsInt(bean interface{}, colNames ...string) ([]int64, error)
Table(tableNameOrBean interface{}) *Session
Unscoped() *Session
Update(bean interface{}, condiBeans ...interface{}) (int64, error)
UseBool(...string) *Session
Where(interface{}, ...interface{}) *Session
}

// EngineInterface defines the interface which Engine, EngineGroup will implementate.
type EngineInterface interface {
Interface

Before(func(interface{})) *Session
Charset(charset string) *Session
CreateTables(...interface{}) error
DBMetas() ([]*core.Table, error)
Dialect() core.Dialect
DropTables(...interface{}) error
DumpAllToFile(fp string, tp ...core.DbType) error
GetColumnMapper() core.IMapper
GetDefaultCacher() core.Cacher
GetTableMapper() core.IMapper
GetTZDatabase() *time.Location
GetTZLocation() *time.Location
NewSession() *Session
NoAutoTime() *Session
Quote(string) string
SetDefaultCacher(core.Cacher)
SetLogLevel(core.LogLevel)
SetMapper(core.IMapper)
SetTZDatabase(tz *time.Location)
SetTZLocation(tz *time.Location)
ShowSQL(show ...bool)
Sync(...interface{}) error
Sync2(...interface{}) error
StoreEngine(storeEngine string) *Session
TableInfo(bean interface{}) *Table
UnMapType(reflect.Type)
}

var (
_ Interface = &Session{}
_ EngineInterface = &Engine{}
_ EngineInterface = &EngineGroup{}
)

+ 3
- 2
session.go View File

@@ -76,6 +76,7 @@ func (session *Session) Init() {
session.afterDeleteBeans = make(map[interface{}]*[]func(interface{}), 0)
session.beforeClosures = make([]func(interface{}), 0)
session.afterClosures = make([]func(interface{}), 0)
session.stmtCache = make(map[uint32]*core.Stmt)

session.afterProcessors = make([]executedProcessor, 0)

@@ -262,13 +263,13 @@ func (session *Session) canCache() bool {
return true
}

func (session *Session) doPrepare(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))
// TODO try hash(sqlStr+len(sqlStr))
var has bool
stmt, has = session.stmtCache[crc]
if !has {
stmt, err = session.DB().Prepare(sqlStr)
stmt, err = db.Prepare(sqlStr)
if err != nil {
return nil, err
}

+ 1
- 1
session_cols_test.go View File

@@ -28,7 +28,7 @@ func TestSetExpr(t *testing.T) {
assert.EqualValues(t, 1, cnt)

var not = "NOT"
if testEngine.dialect.DBType() == core.MSSQL {
if testEngine.Dialect().DBType() == core.MSSQL {
not = "~"
}
cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))

+ 2
- 2
session_cond_test.go View File

@@ -183,7 +183,7 @@ func TestIn(t *testing.T) {
idsInterface = append(idsInterface, id)
}

department := "`" + testEngine.ColumnMapper.Obj2Table("Departname") + "`"
department := "`" + testEngine.GetColumnMapper().Obj2Table("Departname") + "`"
err = testEngine.Where(department+" = ?", "dev").In("(id)", idsInterface...).Find(&users)
if err != nil {
t.Error(err)
@@ -205,7 +205,7 @@ func TestIn(t *testing.T) {
}
}

dev := testEngine.ColumnMapper.Obj2Table("Dev")
dev := testEngine.GetColumnMapper().Obj2Table("Dev")

err = testEngine.In("(id)", 1).In("(id)", 2).In(department, dev).Find(&users)


+ 7
- 7
session_delete_test.go View File

@@ -77,7 +77,7 @@ func TestDeleted(t *testing.T) {

// Test normal Find()
var records1 []Deleted
err = testEngine.Where("`"+testEngine.ColumnMapper.Obj2Table("Id")+"` > 0").Find(&records1, &Deleted{})
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&records1, &Deleted{})
assert.EqualValues(t, 3, len(records1))

// Test normal Get()
@@ -96,7 +96,7 @@ func TestDeleted(t *testing.T) {
assert.False(t, has)

var records2 []Deleted
err = testEngine.Where("`" + testEngine.ColumnMapper.Obj2Table("Id") + "` > 0").Find(&records2)
err = testEngine.Where("`" + testEngine.GetColumnMapper().Obj2Table("Id") + "` > 0").Find(&records2)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(records2))

@@ -117,7 +117,7 @@ func TestDeleted(t *testing.T) {

// Test find all records whatever `deleted`.
var unscopedRecords1 []Deleted
err = testEngine.Unscoped().Where("`"+testEngine.ColumnMapper.Obj2Table("Id")+"` > 0").Find(&unscopedRecords1, &Deleted{})
err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords1, &Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 3, len(unscopedRecords1))

@@ -127,13 +127,13 @@ func TestDeleted(t *testing.T) {
assert.EqualValues(t, 1, affected)

var unscopedRecords2 []Deleted
err = testEngine.Unscoped().Where("`"+testEngine.ColumnMapper.Obj2Table("Id")+"` > 0").Find(&unscopedRecords2, &Deleted{})
err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords2, &Deleted{})
assert.NoError(t, err)
assert.EqualValues(t, 2, len(unscopedRecords2))

var records3 []Deleted
err = testEngine.Where("`"+testEngine.ColumnMapper.Obj2Table("Id")+"` > 0").And("`"+testEngine.ColumnMapper.Obj2Table("Id")+"`> 1").
Or("`"+testEngine.ColumnMapper.Obj2Table("Id")+"` = ?", 3).Find(&records3)
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").And("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"`> 1").
Or("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` = ?", 3).Find(&records3)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(records3))
}
@@ -141,7 +141,7 @@ func TestDeleted(t *testing.T) {
func TestCacheDelete(t *testing.T) {
assert.NoError(t, prepareEngine())

oldCacher := testEngine.Cacher
oldCacher := testEngine.GetDefaultCacher()
cacher := NewLRUCacher(NewMemoryStore(), 1000)
testEngine.SetDefaultCacher(cacher)


+ 21
- 21
session_find_test.go View File

@@ -64,7 +64,7 @@ func TestJoinLimit(t *testing.T) {
func assertSync(t *testing.T, beans ...interface{}) {
for _, bean := range beans {
assert.NoError(t, testEngine.DropTables(bean))
assert.NoError(t, testEngine.Sync(bean))
assert.NoError(t, testEngine.Sync2(bean))
}
}

@@ -105,8 +105,8 @@ func TestFind(t *testing.T) {
}

users2 := make([]Userinfo, 0)
userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
err = testEngine.Sql("select * from " + testEngine.Quote(userinfo)).Find(&users2)
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
err = testEngine.SQL("select * from " + testEngine.Quote(userinfo)).Find(&users2)
if err != nil {
t.Error(err)
panic(err)
@@ -199,7 +199,7 @@ func TestDistinct(t *testing.T) {
assert.NoError(t, err)

users := make([]Userinfo, 0)
departname := testEngine.TableMapper.Obj2Table("Departname")
departname := testEngine.GetTableMapper().Obj2Table("Departname")
err = testEngine.Distinct(departname).Find(&users)
if err != nil {
t.Error(err)
@@ -273,13 +273,13 @@ func TestHaving(t *testing.T) {

func TestOrderSameMapper(t *testing.T) {
assert.NoError(t, prepareEngine())
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())

mapper := testEngine.TableMapper
mapper := testEngine.GetTableMapper()
testEngine.SetMapper(core.SameMapper{})

defer func() {
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.SetMapper(mapper)
}()

@@ -304,12 +304,12 @@ func TestOrderSameMapper(t *testing.T) {

func TestHavingSameMapper(t *testing.T) {
assert.NoError(t, prepareEngine())
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())

mapper := testEngine.TableMapper
mapper := testEngine.GetTableMapper()
testEngine.SetMapper(core.SameMapper{})
defer func() {
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.SetMapper(mapper)
}()
assertSync(t, new(Userinfo))
@@ -326,7 +326,7 @@ func TestFindInts(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
var idsInt64 []int64
err := testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt64)
if err != nil {
@@ -367,8 +367,8 @@ func TestFindInts(t *testing.T) {
func TestFindStrings(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
username := testEngine.ColumnMapper.Obj2Table("Username")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
username := testEngine.GetColumnMapper().Obj2Table("Username")
var idsString []string
err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsString)
if err != nil {
@@ -380,8 +380,8 @@ func TestFindStrings(t *testing.T) {
func TestFindMyString(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
username := testEngine.ColumnMapper.Obj2Table("Username")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
username := testEngine.GetColumnMapper().Obj2Table("Username")

var idsMyString []MyString
err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsMyString)
@@ -395,8 +395,8 @@ func TestFindInterface(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
username := testEngine.ColumnMapper.Obj2Table("Username")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
username := testEngine.GetColumnMapper().Obj2Table("Username")
var idsInterface []interface{}
err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsInterface)
if err != nil {
@@ -409,7 +409,7 @@ func TestFindSliceBytes(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
var ids [][][]byte
err := testEngine.Table(userinfo).Desc("id").Find(&ids)
if err != nil {
@@ -424,7 +424,7 @@ func TestFindSlicePtrString(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
var ids [][]*string
err := testEngine.Table(userinfo).Desc("id").Find(&ids)
if err != nil {
@@ -439,7 +439,7 @@ func TestFindMapBytes(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
var ids []map[string][]byte
err := testEngine.Table(userinfo).Desc("id").Find(&ids)
if err != nil {
@@ -454,7 +454,7 @@ func TestFindMapPtrString(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))

userinfo := testEngine.TableMapper.Obj2Table("Userinfo")
userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
var ids []map[string]*string
err := testEngine.Table(userinfo).Desc("id").Find(&ids)
assert.NoError(t, err)

+ 5
- 5
session_get_test.go View File

@@ -72,7 +72,7 @@ func TestGetVar(t *testing.T) {
assert.Equal(t, "1.5", valuesString["money"])

// for mymysql driver, interface{} will be []byte, so ignore it currently
if testEngine.dialect.DriverName() != "mymysql" {
if testEngine.Dialect().DriverName() != "mymysql" {
var valuesInter = make(map[string]interface{})
has, err = testEngine.Table("get_var").Where("id = ?", 1).Select("*").Get(&valuesInter)
assert.NoError(t, err)
@@ -121,10 +121,10 @@ func TestGetStruct(t *testing.T) {
IsMan bool
}

assert.NoError(t, testEngine.Sync(new(UserinfoGet)))
assert.NoError(t, testEngine.Sync2(new(UserinfoGet)))

var err error
if testEngine.dialect.DBType() == core.MSSQL {
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT userinfo_get ON")
assert.NoError(t, err)
}
@@ -143,9 +143,9 @@ func TestGetStruct(t *testing.T) {
Total int64
}

assert.NoError(t, testEngine.Sync(&NoIdUser{}))
assert.NoError(t, testEngine.Sync2(&NoIdUser{}))

userCol := testEngine.ColumnMapper.Obj2Table("User")
userCol := testEngine.GetColumnMapper().Obj2Table("User")
_, err = testEngine.Where("`"+userCol+"` = ?", "xlw").Delete(&NoIdUser{})
assert.NoError(t, err)


+ 2
- 2
session_insert_test.go View File

@@ -219,7 +219,7 @@ func TestInsertDefault(t *testing.T) {
}

var di2 = DefaultInsert{Name: "test"}
_, err = testEngine.Omit(testEngine.ColumnMapper.Obj2Table("Status")).Insert(&di2)
_, err = testEngine.Omit(testEngine.GetColumnMapper().Obj2Table("Status")).Insert(&di2)
if err != nil {
t.Error(err)
}
@@ -267,7 +267,7 @@ func TestInsertDefault2(t *testing.T) {
}

var di2 = DefaultInsert2{Name: "test"}
_, err = testEngine.Omit(testEngine.ColumnMapper.Obj2Table("CheckTime")).Insert(&di2)
_, err = testEngine.Omit(testEngine.GetColumnMapper().Obj2Table("CheckTime")).Insert(&di2)
if err != nil {
t.Error(err)
}

+ 10
- 3
session_raw.go View File

@@ -47,9 +47,16 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
}

if session.isAutoCommit {
var db *core.DB
if session.engine.engineGroup != nil {
db = session.engine.engineGroup.Slave().DB()
} else {
db = session.DB()
}

if session.prepareStmt {
// don't clear stmt since session will cache them
stmt, err := session.doPrepare(sqlStr)
stmt, err := session.doPrepare(db, sqlStr)
if err != nil {
return nil, err
}
@@ -61,7 +68,7 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
return rows, nil
}

rows, err := session.DB().Query(sqlStr, args...)
rows, err := db.Query(sqlStr, args...)
if err != nil {
return nil, err
}
@@ -171,7 +178,7 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
}

if session.prepareStmt {
stmt, err := session.doPrepare(sqlStr)
stmt, err := session.doPrepare(session.DB(), sqlStr)
if err != nil {
return nil, err
}

+ 1
- 1
session_raw_test.go View File

@@ -19,7 +19,7 @@ func TestQuery(t *testing.T) {
Name string
}

assert.NoError(t, testEngine.Sync(new(UserinfoQuery)))
assert.NoError(t, testEngine.Sync2(new(UserinfoQuery)))

res, err := testEngine.Exec("INSERT INTO `userinfo_query` (uid, name) VALUES (?, ?)", 1, "user")
assert.NoError(t, err)

+ 2
- 2
session_schema_test.go View File

@@ -126,13 +126,13 @@ func TestIsTableEmpty(t *testing.T) {

assert.NoError(t, testEngine.DropTables(&PictureEmpty{}, &NumericEmpty{}))

assert.NoError(t, testEngine.Sync(new(PictureEmpty), new(NumericEmpty)))
assert.NoError(t, testEngine.Sync2(new(PictureEmpty), new(NumericEmpty)))

isEmpty, err := testEngine.IsTableEmpty(&PictureEmpty{})
assert.NoError(t, err)
assert.True(t, isEmpty)

tbName := testEngine.TableMapper.Obj2Table("PictureEmpty")
tbName := testEngine.GetTableMapper().Obj2Table("PictureEmpty")
isEmpty, err = testEngine.IsTableEmpty(tbName)
assert.NoError(t, err)
assert.True(t, isEmpty)

+ 3
- 3
session_stats_test.go View File

@@ -46,8 +46,8 @@ func TestSum(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 3, cnt)

colInt := testEngine.ColumnMapper.Obj2Table("Int")
colFloat := testEngine.ColumnMapper.Obj2Table("Float")
colInt := testEngine.GetColumnMapper().Obj2Table("Int")
colFloat := testEngine.GetColumnMapper().Obj2Table("Float")

sumInt, err := testEngine.Sum(new(SumStruct), colInt)
assert.NoError(t, err)
@@ -109,7 +109,7 @@ func TestCount(t *testing.T) {
}
assert.NoError(t, testEngine.Sync2(new(UserinfoCount)))

colName := testEngine.ColumnMapper.Obj2Table("Departname")
colName := testEngine.GetColumnMapper().Obj2Table("Departname")
var cond builder.Cond = builder.Eq{
"`" + colName + "`": "dev",
}

+ 3
- 3
session_tx_test.go View File

@@ -128,11 +128,11 @@ func TestCombineTransaction(t *testing.T) {
func TestCombineTransactionSameMapper(t *testing.T) {
assert.NoError(t, prepareEngine())

oldMapper := testEngine.ColumnMapper
testEngine.unMapType(rValue(new(Userinfo)).Type())
oldMapper := testEngine.GetColumnMapper()
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.SetMapper(core.SameMapper{})
defer func() {
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.SetMapper(oldMapper)
}()


+ 20
- 20
session_update_test.go View File

@@ -83,7 +83,7 @@ type ForUpdate struct {
Name string
}

func setupForUpdate(engine *Engine) error {
func setupForUpdate(engine EngineInterface) error {
v := new(ForUpdate)
err := testEngine.DropTables(v)
if err != nil {
@@ -110,7 +110,7 @@ func setupForUpdate(engine *Engine) error {
}

func TestForUpdate(t *testing.T) {
if testEngine.DriverName() != "mysql" && testEngine.DriverName() != "mymysql" {
if testEngine.Dialect().DriverName() != "mysql" && testEngine.Dialect().DriverName() != "mymysql" {
return
}

@@ -517,8 +517,8 @@ func TestUpdate1(t *testing.T) {
}

col2 := &UpdateMustCols{col1.Id, true, ""}
boolStr := testEngine.ColumnMapper.Obj2Table("Bool")
stringStr := testEngine.ColumnMapper.Obj2Table("String")
boolStr := testEngine.GetColumnMapper().Obj2Table("Bool")
stringStr := testEngine.GetColumnMapper().Obj2Table("String")
_, err = testEngine.ID(col2.Id).MustCols(boolStr, stringStr).Update(col2)
if err != nil {
t.Error(err)
@@ -559,7 +559,7 @@ func TestUpdateIncrDecr(t *testing.T) {
_, err := testEngine.Insert(col1)
assert.NoError(t, err)

colName := testEngine.ColumnMapper.Obj2Table("Cnt")
colName := testEngine.GetColumnMapper().Obj2Table("Cnt")

cnt, err := testEngine.ID(col1.Id).Incr(colName).Update(col1)
assert.NoError(t, err)
@@ -759,21 +759,21 @@ func TestUpdateUpdated(t *testing.T) {
func TestUpdateSameMapper(t *testing.T) {
assert.NoError(t, prepareEngine())

oldMapper := testEngine.ColumnMapper
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.unMapType(rValue(new(Condi)).Type())
testEngine.unMapType(rValue(new(Article)).Type())
testEngine.unMapType(rValue(new(UpdateAllCols)).Type())
testEngine.unMapType(rValue(new(UpdateMustCols)).Type())
testEngine.unMapType(rValue(new(UpdateIncr)).Type())
oldMapper := testEngine.GetColumnMapper()
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Condi)).Type())
testEngine.UnMapType(rValue(new(Article)).Type())
testEngine.UnMapType(rValue(new(UpdateAllCols)).Type())
testEngine.UnMapType(rValue(new(UpdateMustCols)).Type())
testEngine.UnMapType(rValue(new(UpdateIncr)).Type())
testEngine.SetMapper(core.SameMapper{})
defer func() {
testEngine.unMapType(rValue(new(Userinfo)).Type())
testEngine.unMapType(rValue(new(Condi)).Type())
testEngine.unMapType(rValue(new(Article)).Type())
testEngine.unMapType(rValue(new(UpdateAllCols)).Type())
testEngine.unMapType(rValue(new(UpdateMustCols)).Type())
testEngine.unMapType(rValue(new(UpdateIncr)).Type())
testEngine.UnMapType(rValue(new(Userinfo)).Type())
testEngine.UnMapType(rValue(new(Condi)).Type())
testEngine.UnMapType(rValue(new(Article)).Type())
testEngine.UnMapType(rValue(new(UpdateAllCols)).Type())
testEngine.UnMapType(rValue(new(UpdateMustCols)).Type())
testEngine.UnMapType(rValue(new(UpdateIncr)).Type())
testEngine.SetMapper(oldMapper)
}()

@@ -943,8 +943,8 @@ func TestUpdateSameMapper(t *testing.T) {
}

col2 := &UpdateMustCols{col1.Id, true, ""}
boolStr := testEngine.ColumnMapper.Obj2Table("Bool")
stringStr := testEngine.ColumnMapper.Obj2Table("String")
boolStr := testEngine.GetColumnMapper().Obj2Table("Bool")
stringStr := testEngine.GetColumnMapper().Obj2Table("String")
_, err = testEngine.ID(col2.Id).MustCols(boolStr, stringStr).Update(col2)
if err != nil {
t.Error(err)

+ 16
- 6
statement_test.go View File

@@ -163,10 +163,20 @@ func (TestType) TableName() string {
}

func createTestStatement() *Statement {
statement := &Statement{}
statement.Init()
statement.Engine = testEngine
statement.setRefValue(reflect.ValueOf(TestType{}))

return statement
if engine, ok := testEngine.(*Engine); ok {
statement := &Statement{}
statement.Init()
statement.Engine = engine
statement.setRefValue(reflect.ValueOf(TestType{}))

return statement
} else if eg, ok := testEngine.(*EngineGroup); ok {
statement := &Statement{}
statement.Init()
statement.Engine = eg.Engine
statement.setRefValue(reflect.ValueOf(TestType{}))

return statement
}
return nil
}

+ 10
- 10
tag_extends_test.go View File

@@ -202,13 +202,13 @@ func TestExtends(t *testing.T) {

var info UserAndDetail
qt := testEngine.Quote
ui := testEngine.TableMapper.Obj2Table("Userinfo")
ud := testEngine.TableMapper.Obj2Table("Userdetail")
uiid := testEngine.TableMapper.Obj2Table("Id")
ui := testEngine.GetTableMapper().Obj2Table("Userinfo")
ud := testEngine.GetTableMapper().Obj2Table("Userdetail")
uiid := testEngine.GetTableMapper().Obj2Table("Id")
udid := "detail_id"
sql := fmt.Sprintf("select * from %s, %s where %s.%s = %s.%s",
qt(ui), qt(ud), qt(ui), qt(udid), qt(ud), qt(uiid))
b, err := testEngine.Sql(sql).NoCascade().Get(&info)
b, err := testEngine.SQL(sql).NoCascade().Get(&info)
if err != nil {
t.Error(err)
panic(err)
@@ -329,7 +329,7 @@ func TestExtends2(t *testing.T) {
Uid: sender.Id,
ToUid: receiver.Id,
}
if testEngine.dialect.DBType() == core.MSSQL {
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
@@ -340,7 +340,7 @@ func TestExtends2(t *testing.T) {
panic(err)
}

var mapper = testEngine.TableMapper.Obj2Table
var mapper = testEngine.GetTableMapper().Obj2Table
userTableName := mapper("MessageUser")
typeTableName := mapper("MessageType")
msgTableName := mapper("Message")
@@ -401,7 +401,7 @@ func TestExtends3(t *testing.T) {
Uid: sender.Id,
ToUid: receiver.Id,
}
if testEngine.dialect.DBType() == core.MSSQL {
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
@@ -411,7 +411,7 @@ func TestExtends3(t *testing.T) {
panic(err)
}

var mapper = testEngine.TableMapper.Obj2Table
var mapper = testEngine.GetTableMapper().Obj2Table
userTableName := mapper("MessageUser")
typeTableName := mapper("MessageType")
msgTableName := mapper("Message")
@@ -488,7 +488,7 @@ func TestExtends4(t *testing.T) {
Content: "test",
Uid: sender.Id,
}
if testEngine.dialect.DBType() == core.MSSQL {
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT message ON")
assert.NoError(t, err)
}
@@ -498,7 +498,7 @@ func TestExtends4(t *testing.T) {
panic(err)
}

var mapper = testEngine.TableMapper.Obj2Table
var mapper = testEngine.GetTableMapper().Obj2Table
userTableName := mapper("MessageUser")
typeTableName := mapper("MessageType")
msgTableName := mapper("Message")

+ 6
- 6
tag_id_test.go View File

@@ -18,11 +18,11 @@ type IDGonicMapper struct {
func TestGonicMapperID(t *testing.T) {
assert.NoError(t, prepareEngine())

oldMapper := testEngine.ColumnMapper
testEngine.unMapType(rValue(new(IDGonicMapper)).Type())
oldMapper := testEngine.GetColumnMapper()
testEngine.UnMapType(rValue(new(IDGonicMapper)).Type())
testEngine.SetMapper(core.LintGonicMapper)
defer func() {
testEngine.unMapType(rValue(new(IDGonicMapper)).Type())
testEngine.UnMapType(rValue(new(IDGonicMapper)).Type())
testEngine.SetMapper(oldMapper)
}()

@@ -55,11 +55,11 @@ type IDSameMapper struct {
func TestSameMapperID(t *testing.T) {
assert.NoError(t, prepareEngine())

oldMapper := testEngine.ColumnMapper
testEngine.unMapType(rValue(new(IDSameMapper)).Type())
oldMapper := testEngine.GetColumnMapper()
testEngine.UnMapType(rValue(new(IDSameMapper)).Type())
testEngine.SetMapper(core.SameMapper{})
defer func() {
testEngine.unMapType(rValue(new(IDSameMapper)).Type())
testEngine.UnMapType(rValue(new(IDSameMapper)).Type())
testEngine.SetMapper(oldMapper)
}()


+ 4
- 4
tag_test.go View File

@@ -123,7 +123,7 @@ func TestCreatedUpdated(t *testing.T) {
Updated time.Time `xorm:"updated"`
}

err := testEngine.Sync(&CreatedUpdated{})
err := testEngine.Sync2(&CreatedUpdated{})
assert.NoError(t, err)

c := &CreatedUpdated{Name: "test"}
@@ -178,7 +178,7 @@ type Lowercase struct {
func TestLowerCase(t *testing.T) {
assert.NoError(t, prepareEngine())

err := testEngine.Sync(&Lowercase{})
err := testEngine.Sync2(&Lowercase{})
_, err = testEngine.Where("(id) > 0").Delete(&Lowercase{})
if err != nil {
t.Error(err)
@@ -255,7 +255,7 @@ func TestAutoIncrTag(t *testing.T) {
func TestTagComment(t *testing.T) {
assert.NoError(t, prepareEngine())
// FIXME: only support mysql
if testEngine.dialect.DriverName() != core.MYSQL {
if testEngine.Dialect().DriverName() != core.MYSQL {
return
}

@@ -371,7 +371,7 @@ func TestTagTime(t *testing.T) {

assertSync(t, new(TagUTCStruct))

assert.EqualValues(t, time.Local.String(), testEngine.TZLocation.String())
assert.EqualValues(t, time.Local.String(), testEngine.GetTZLocation().String())

s := TagUTCStruct{
Name: "utc",

+ 10
- 10
time_test.go View File

@@ -47,10 +47,10 @@ func TestTimeUserTimeDiffLoc(t *testing.T) {
assert.NoError(t, prepareEngine())
loc, err := time.LoadLocation("Asia/Shanghai")
assert.NoError(t, err)
testEngine.TZLocation = loc
testEngine.SetTZLocation(loc)
dbLoc, err := time.LoadLocation("America/New_York")
assert.NoError(t, err)
testEngine.DatabaseTZ = dbLoc
testEngine.SetTZDatabase(dbLoc)

type TimeUser2 struct {
Id string
@@ -112,10 +112,10 @@ func TestTimeUserCreatedDiffLoc(t *testing.T) {
assert.NoError(t, prepareEngine())
loc, err := time.LoadLocation("Asia/Shanghai")
assert.NoError(t, err)
testEngine.TZLocation = loc
testEngine.SetTZLocation(loc)
dbLoc, err := time.LoadLocation("America/New_York")
assert.NoError(t, err)
testEngine.DatabaseTZ = dbLoc
testEngine.SetTZDatabase(dbLoc)

type UserCreated2 struct {
Id string
@@ -198,10 +198,10 @@ func TestTimeUserUpdatedDiffLoc(t *testing.T) {
assert.NoError(t, prepareEngine())
loc, err := time.LoadLocation("Asia/Shanghai")
assert.NoError(t, err)
testEngine.TZLocation = loc
testEngine.SetTZLocation(loc)
dbLoc, err := time.LoadLocation("America/New_York")
assert.NoError(t, err)
testEngine.DatabaseTZ = dbLoc
testEngine.SetTZDatabase(dbLoc)

type UserUpdated2 struct {
Id string
@@ -302,10 +302,10 @@ func TestTimeUserDeletedDiffLoc(t *testing.T) {
assert.NoError(t, prepareEngine())
loc, err := time.LoadLocation("Asia/Shanghai")
assert.NoError(t, err)
testEngine.TZLocation = loc
testEngine.SetTZLocation(loc)
dbLoc, err := time.LoadLocation("America/New_York")
assert.NoError(t, err)
testEngine.DatabaseTZ = dbLoc
testEngine.SetTZDatabase(dbLoc)

type UserDeleted2 struct {
Id string
@@ -426,10 +426,10 @@ func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {
assert.NoError(t, prepareEngine())
loc, err := time.LoadLocation("Asia/Shanghai")
assert.NoError(t, err)
testEngine.TZLocation = loc
testEngine.SetTZLocation(loc)
dbLoc, err := time.LoadLocation("America/New_York")
assert.NoError(t, err)
testEngine.DatabaseTZ = dbLoc
testEngine.SetTZDatabase(dbLoc)

type UserDeleted4 struct {
Id string

+ 3
- 3
types_test.go View File

@@ -154,7 +154,7 @@ func TestConversion(t *testing.T) {

c := new(ConvStruct)
assert.NoError(t, testEngine.DropTables(c))
assert.NoError(t, testEngine.Sync(c))
assert.NoError(t, testEngine.Sync2(c))

var s ConvString = "sssss"
c.Conv = "tttt"
@@ -304,7 +304,7 @@ func TestCustomType2(t *testing.T) {
err := testEngine.CreateTables(&UserCus{})
assert.NoError(t, err)

tableName := testEngine.TableMapper.Obj2Table("UserCus")
tableName := testEngine.GetTableMapper().Obj2Table("UserCus")
_, err = testEngine.Exec("delete from " + testEngine.Quote(tableName))
assert.NoError(t, err)

@@ -327,7 +327,7 @@ func TestCustomType2(t *testing.T) {
fmt.Println(user)

users := make([]UserCus, 0)
err = testEngine.Where("`"+testEngine.ColumnMapper.Obj2Table("Status")+"` = ?", "Registed").Find(&users)
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Status")+"` = ?", "Registed").Find(&users)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(users))


+ 11
- 3
xorm_test.go View File

@@ -16,7 +16,7 @@ import (
)

var (
testEngine *Engine
testEngine EngineInterface
dbType string
connString string

@@ -25,18 +25,26 @@ var (
ptrConnStr = flag.String("conn_str", "./test.db?cache=shared&mode=rwc", "test database connection string")
mapType = flag.String("map_type", "snake", "indicate the name mapping")
cache = flag.Bool("cache", false, "if enable cache")
cluster = flag.Bool("cluster", false, "if this is a cluster")
splitter = flag.String("splitter", ";", "the splitter on connstr for cluster")
)

func createEngine(dbType, connStr string) error {
if testEngine == nil {
var err error
testEngine, err = NewEngine(dbType, connStr)

if !*cluster {
testEngine, err = NewEngine(dbType, connStr)

} else {
testEngine, err = NewEngineGroup(dbType, strings.Split(connStr, *splitter))
}
if err != nil {
return err
}

testEngine.ShowSQL(*showSQL)
testEngine.logger.SetLevel(core.LOG_DEBUG)
testEngine.SetLogLevel(core.LOG_DEBUG)
if *cache {
cacher := NewLRUCacher(NewMemoryStore(), 100000)
testEngine.SetDefaultCacher(cacher)

Loading…
Cancel
Save