improved docs
This commit is contained in:
parent
58dec3a47c
commit
5113f5d35b
26
engine.go
26
engine.go
|
@ -18,6 +18,7 @@ const (
|
||||||
MYMYSQL = "mymysql"
|
MYMYSQL = "mymysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// a dialect is a driver's wrapper
|
||||||
type dialect interface {
|
type dialect interface {
|
||||||
Init(uri string) error
|
Init(uri string) error
|
||||||
SqlType(t *Column) string
|
SqlType(t *Column) string
|
||||||
|
@ -50,36 +51,47 @@ type Engine struct {
|
||||||
UseCache bool
|
UseCache bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If engine's database support batch insert records like
|
||||||
|
// "insert into user values (name, age), (name, age)".
|
||||||
|
// When the return is ture, then engine.Insert(&users) will
|
||||||
|
// generate batch sql and exeute.
|
||||||
func (engine *Engine) SupportInsertMany() bool {
|
func (engine *Engine) SupportInsertMany() bool {
|
||||||
return engine.Dialect.SupportInsertMany()
|
return engine.Dialect.SupportInsertMany()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Engine's database use which charactor as quote.
|
||||||
|
// mysql, sqlite use ` and postgres use "
|
||||||
func (engine *Engine) QuoteStr() string {
|
func (engine *Engine) QuoteStr() string {
|
||||||
return engine.Dialect.QuoteStr()
|
return engine.Dialect.QuoteStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use QuoteStr quote the string sql
|
||||||
func (engine *Engine) Quote(sql string) string {
|
func (engine *Engine) Quote(sql string) string {
|
||||||
return engine.Dialect.QuoteStr() + sql + engine.Dialect.QuoteStr()
|
return engine.Dialect.QuoteStr() + sql + engine.Dialect.QuoteStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A simple wrapper to dialect's SqlType method
|
||||||
func (engine *Engine) SqlType(c *Column) string {
|
func (engine *Engine) SqlType(c *Column) string {
|
||||||
return engine.Dialect.SqlType(c)
|
return engine.Dialect.SqlType(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database's autoincrement statement
|
||||||
func (engine *Engine) AutoIncrStr() string {
|
func (engine *Engine) AutoIncrStr() string {
|
||||||
return engine.Dialect.AutoIncrStr()
|
return engine.Dialect.AutoIncrStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set engine's pool, the pool default is Go's standard library's connection pool.
|
||||||
func (engine *Engine) SetPool(pool IConnectPool) error {
|
func (engine *Engine) SetPool(pool IConnectPool) error {
|
||||||
engine.Pool = pool
|
engine.Pool = pool
|
||||||
return engine.Pool.Init(engine)
|
return engine.Pool.Init(engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
// only for go 1.2+
|
// SetMaxConns is only available for go 1.2+
|
||||||
func (engine *Engine) SetMaxConns(conns int) {
|
func (engine *Engine) SetMaxConns(conns int) {
|
||||||
engine.Pool.SetMaxConns(conns)
|
engine.Pool.SetMaxConns(conns)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDefaltCacher set the default cacher. Xorm's default not enable cacher.
|
||||||
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
||||||
if cacher == nil {
|
if cacher == nil {
|
||||||
engine.UseCache = false
|
engine.UseCache = false
|
||||||
|
@ -89,33 +101,39 @@ func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If you has set default cacher, and you want temporilly stop use cache,
|
||||||
|
// you can use NoCache()
|
||||||
func (engine *Engine) NoCache() *Session {
|
func (engine *Engine) NoCache() *Session {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
session.IsAutoClose = true
|
session.IsAutoClose = true
|
||||||
return session.NoCache()
|
return session.NoCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set a table use a special cacher
|
||||||
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
||||||
t := rType(bean)
|
t := rType(bean)
|
||||||
engine.AutoMapType(t)
|
engine.AutoMapType(t)
|
||||||
engine.Tables[t].Cacher = cacher
|
engine.Tables[t].Cacher = cacher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenDB provides a interface to operate database directly.
|
||||||
func (engine *Engine) OpenDB() (*sql.DB, error) {
|
func (engine *Engine) OpenDB() (*sql.DB, error) {
|
||||||
return sql.Open(engine.DriverName, engine.DataSourceName)
|
return sql.Open(engine.DriverName, engine.DataSourceName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New a session
|
||||||
func (engine *Engine) NewSession() *Session {
|
func (engine *Engine) NewSession() *Session {
|
||||||
session := &Session{Engine: engine}
|
session := &Session{Engine: engine}
|
||||||
session.Init()
|
session.Init()
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the engine
|
||||||
func (engine *Engine) Close() error {
|
func (engine *Engine) Close() error {
|
||||||
return engine.Pool.Close(engine)
|
return engine.Pool.Close(engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if databse is alive.
|
// Test if database is alive.
|
||||||
func (engine *Engine) Test() error {
|
func (engine *Engine) Test() error {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
@ -449,7 +467,7 @@ func (engine *Engine) MapType(t reflect.Type) *Table {
|
||||||
return table
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map should use before all operation because it's not thread safe
|
// Map a struct to a table
|
||||||
func (engine *Engine) Map(beans ...interface{}) (e error) {
|
func (engine *Engine) Map(beans ...interface{}) (e error) {
|
||||||
engine.mutex.Lock()
|
engine.mutex.Lock()
|
||||||
defer engine.mutex.Unlock()
|
defer engine.mutex.Unlock()
|
||||||
|
@ -461,7 +479,7 @@ func (engine *Engine) Map(beans ...interface{}) (e error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is a table has any reocrd
|
// Is a table has any reocrd
|
||||||
func (engine *Engine) IsEmptyTable(bean interface{}) (bool, error) {
|
func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error) {
|
||||||
t := rType(bean)
|
t := rType(bean)
|
||||||
if t.Kind() != reflect.Struct {
|
if t.Kind() != reflect.Struct {
|
||||||
return false, errors.New("bean should be a struct or struct's point")
|
return false, errors.New("bean should be a struct or struct's point")
|
||||||
|
|
Loading…
Reference in New Issue