Browse Source

add tests support for EngineGroup & fix some bugs

xormplus 8 years ago
parent
commit
95a2e9a27e
2 changed files with 155 additions and 15 deletions
  1. 56 15
      engine.go
  2. 99 0
      interface.go

+ 56 - 15
engine.go

@@ -54,6 +54,21 @@ type Engine struct {
 	engineGroup *EngineGroup
 	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
 // 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...)
@@ -84,6 +99,11 @@ func (engine *Engine) SetLogger(logger core.ILogger) {
 	engine.dialect.SetLogger(logger)
 	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
 // SetDisableGlobalCache disable global cache or not
 func (engine *Engine) SetDisableGlobalCache(disable bool) {
 func (engine *Engine) SetDisableGlobalCache(disable bool) {
 	if engine.disableGlobalCache != disable {
 	if engine.disableGlobalCache != disable {
@@ -206,6 +226,11 @@ func (engine *Engine) SetDefaultCacher(cacher core.Cacher) {
 	engine.Cacher = 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,
 // NoCache If you has set default cacher, and you want temporilly stop use cache,
 // you can use NoCache()
 // you can use NoCache()
 func (engine *Engine) NoCache() *Session {
 func (engine *Engine) NoCache() *Session {
@@ -768,7 +793,8 @@ func (engine *Engine) Having(conditions string) *Session {
 	return session.Having(conditions)
 	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()
 	engine.mutex.Lock()
 	defer engine.mutex.Unlock()
 	defer engine.mutex.Unlock()
 	delete(engine.Tables, t)
 	delete(engine.Tables, t)
@@ -1574,24 +1600,39 @@ func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}
 	return
 	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 := engine.NewSession()
 	session.isAutoClose = true
 	session.isAutoClose = true
-	return session.BufferSize(size)
+	return session.Unscoped()
 }
 }

+ 99 - 0
interface.go

@@ -0,0 +1,99 @@
+// 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/xormplus/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
+	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
+	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)
+	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)
+	Sync2(...interface{}) error
+	StoreEngine(storeEngine string) *Session
+	TableInfo(bean interface{}) *Table
+	UnMapType(reflect.Type)
+}
+
+var (
+	_ Interface       = &Session{}
+	_ EngineInterface = &Engine{}
+	_ EngineInterface = &EngineGroup{}
+)