2016-11-03 18:16:01 -04:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
// +build go1.11
|
2018-12-11 20:01:41 -05:00
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
2019-06-23 11:22:43 -04:00
|
|
|
"context"
|
2016-11-03 18:16:01 -04:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
"xorm.io/xorm/caches"
|
2020-03-25 10:32:23 -04:00
|
|
|
"xorm.io/xorm/core"
|
2020-03-22 11:12:55 -04:00
|
|
|
"xorm.io/xorm/dialects"
|
|
|
|
"xorm.io/xorm/log"
|
|
|
|
"xorm.io/xorm/names"
|
|
|
|
"xorm.io/xorm/schemas"
|
|
|
|
"xorm.io/xorm/tags"
|
2016-11-03 18:16:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func close(engine *Engine) {
|
|
|
|
engine.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEngine new a db manager according to the parameter. Currently support four
|
|
|
|
// drivers
|
|
|
|
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
2020-03-22 11:12:55 -04:00
|
|
|
dialect, err := dialects.OpenDialect(driverName, dataSourceName)
|
2016-11-03 18:16:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:23 -04:00
|
|
|
db, err := core.Open(driverName, dataSourceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
cacherMgr := caches.NewManager()
|
|
|
|
mapper := names.NewCacheMapper(new(names.SnakeMapper))
|
|
|
|
tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr)
|
2016-11-03 18:16:01 -04:00
|
|
|
|
|
|
|
engine := &Engine{
|
2019-06-23 11:22:43 -04:00
|
|
|
dialect: dialect,
|
|
|
|
TZLocation: time.Local,
|
|
|
|
defaultContext: context.Background(),
|
2020-03-22 11:12:55 -04:00
|
|
|
cacherMgr: cacherMgr,
|
|
|
|
tagParser: tagParser,
|
|
|
|
driverName: driverName,
|
|
|
|
dataSourceName: dataSourceName,
|
2020-03-25 10:32:23 -04:00
|
|
|
db: db,
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
if dialect.URI().DBType == schemas.SQLITE {
|
2017-08-22 07:39:52 -04:00
|
|
|
engine.DatabaseTZ = time.UTC
|
|
|
|
} else {
|
|
|
|
engine.DatabaseTZ = time.Local
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
logger := log.NewSimpleLogger(os.Stdout)
|
|
|
|
logger.SetLevel(log.LOG_INFO)
|
|
|
|
engine.SetLogger(log.NewLoggerAdapter(logger))
|
2016-11-03 18:16:01 -04:00
|
|
|
|
|
|
|
runtime.SetFinalizer(engine, close)
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:12:55 -04:00
|
|
|
// NewEngineWithParams new a db manager with params. The params will be passed to dialects.
|
2018-07-19 22:10:17 -04:00
|
|
|
func NewEngineWithParams(driverName string, dataSourceName string, params map[string]string) (*Engine, error) {
|
|
|
|
engine, err := NewEngine(driverName, dataSourceName)
|
|
|
|
engine.dialect.SetParams(params)
|
|
|
|
return engine, err
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:16:01 -04:00
|
|
|
// Clone clone an engine
|
|
|
|
func (engine *Engine) Clone() (*Engine, error) {
|
|
|
|
return NewEngine(engine.DriverName(), engine.DataSourceName())
|
|
|
|
}
|