示例#1
0
文件: xorm.go 项目: hongzhen/xorm
// new a db manager according to the parameter. Currently support four
// drivers
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
	regDrvsNDialects()
	driver := core.QueryDriver(driverName)
	if driver == nil {
		return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
	}

	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))
	}

	db, err := core.Open(driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	err = dialect.Init(db, uri, driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	engine := &Engine{
		db:            db,
		dialect:       dialect,
		Tables:        make(map[reflect.Type]*core.Table),
		mutex:         &sync.RWMutex{},
		TagIdentifier: "xorm",
		Logger:        NewSimpleLogger(os.Stdout),
		TZLocation:    time.Local,
	}

	engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))

	//engine.Filters = dialect.Filters()
	//engine.Cacher = NewLRUCacher()
	//err = engine.SetPool(NewSysConnectPool())

	runtime.SetFinalizer(engine, close)
	return engine, err
}
示例#2
0
文件: xorm.go 项目: admpub/xorm
// NewEngine new a db manager according to the parameter. Currently support four
// drivers
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
	regDrvsNDialects()
	driver := core.QueryDriver(driverName)
	if driver == nil {
		return nil, fmt.Errorf("Unsupported driver name: %v", driverName)
	}

	uri, err := driver.Parse(driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	dialect := core.QueryDialect(uri.DbType)
	if dialect == nil {
		return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DbType)
	}

	db, err := core.Open(driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	err = dialect.Init(db, uri, driverName, dataSourceName)
	if err != nil {
		return nil, err
	}

	engine := &Engine{
		db:            db,
		dialect:       dialect,
		Tables:        make(map[reflect.Type]*core.Table),
		mutex:         &sync.RWMutex{},
		TagIdentifier: "xorm",
		TZLocation:    time.Local,
	}

	logger := NewSimpleLogger(os.Stdout)
	logger.SetLevel(core.LOG_INFO)
	engine.SetLogger(logger)
	engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper)))

	runtime.SetFinalizer(engine, close)

	return engine, nil
}