2013-05-03 07:26:51 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
2013-12-18 03:31:32 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
2014-01-25 02:07:11 +00:00
|
|
|
"time"
|
2014-01-07 09:33:27 +00:00
|
|
|
|
2014-01-25 02:07:11 +00:00
|
|
|
"github.com/lunny/xorm/caches"
|
2014-01-07 09:33:27 +00:00
|
|
|
"github.com/lunny/xorm/core"
|
|
|
|
_ "github.com/lunny/xorm/dialects"
|
|
|
|
_ "github.com/lunny/xorm/drivers"
|
2013-05-03 07:26:51 +00:00
|
|
|
)
|
|
|
|
|
2013-07-03 03:49:29 +00:00
|
|
|
const (
|
2014-01-02 09:59:34 +00:00
|
|
|
Version string = "0.3.1"
|
2013-07-03 03:49:29 +00:00
|
|
|
)
|
|
|
|
|
2013-09-26 07:19:39 +00:00
|
|
|
func close(engine *Engine) {
|
2013-12-18 03:31:32 +00:00
|
|
|
engine.Close()
|
2013-09-26 07:19:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 15:14:42 +00:00
|
|
|
// new a db manager according to the parameter. Currently support four
|
|
|
|
// drivers
|
2013-06-16 03:05:16 +00:00
|
|
|
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
2014-01-07 09:33:27 +00:00
|
|
|
driver := core.QueryDriver(driverName)
|
|
|
|
if driver == nil {
|
2013-12-18 03:31:32 +00:00
|
|
|
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
|
|
|
}
|
2014-01-07 09:33:27 +00:00
|
|
|
|
|
|
|
uri, err := driver.Parse(driverName, dataSourceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dialect := core.QueryDialect(uri.DbType)
|
|
|
|
if dialect == nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Unsupported dialect type: %v", uri.DbType))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dialect.Init(uri, driverName, dataSourceName)
|
2013-12-18 03:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-07 09:33:27 +00:00
|
|
|
engine := &Engine{DriverName: driverName,
|
|
|
|
DataSourceName: dataSourceName, dialect: dialect,
|
2014-01-25 02:07:11 +00:00
|
|
|
tableCachers: make(map[reflect.Type]core.Cacher)}
|
2014-01-07 09:33:27 +00:00
|
|
|
|
2014-01-25 02:07:11 +00:00
|
|
|
engine.SetMapper(core.SnakeMapper{})
|
2014-01-07 09:33:27 +00:00
|
|
|
|
|
|
|
engine.Filters = dialect.Filters()
|
|
|
|
|
|
|
|
engine.Tables = make(map[reflect.Type]*core.Table)
|
2013-12-18 03:31:32 +00:00
|
|
|
engine.mutex = &sync.Mutex{}
|
|
|
|
engine.TagIdentifier = "xorm"
|
|
|
|
|
|
|
|
engine.Logger = os.Stdout
|
|
|
|
|
|
|
|
//engine.Pool = NewSimpleConnectPool()
|
|
|
|
//engine.Pool = NewNoneConnectPool()
|
|
|
|
//engine.Cacher = NewLRUCacher()
|
|
|
|
err = engine.SetPool(NewSysConnectPool())
|
|
|
|
runtime.SetFinalizer(engine, close)
|
|
|
|
return engine, err
|
2013-05-03 07:26:51 +00:00
|
|
|
}
|
2014-01-25 02:07:11 +00:00
|
|
|
|
|
|
|
func NewLRUCacher(store core.CacheStore, max int) *caches.LRUCacher {
|
|
|
|
return caches.NewLRUCacher(store, core.CacheExpired, core.CacheMaxMemory, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLRUCacher2(store core.CacheStore, expired time.Duration, max int) *caches.LRUCacher {
|
|
|
|
return caches.NewLRUCacher(store, expired, 0, max)
|
|
|
|
}
|