當前位置: 首頁>>代碼示例>>Golang>>正文


Golang xorm.NewEngine函數代碼示例

本文整理匯總了Golang中github.com/lunny/xorm.NewEngine函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewEngine函數的具體用法?Golang NewEngine怎麽用?Golang NewEngine使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewEngine函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: SetEngine

func SetEngine() (err error) {
	switch DbCfg.Type {
	case "mysql":
		orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8",
			DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
	case "postgres":
		orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
			DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
	case "sqlite3":
		os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
		orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
	default:
		return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
	}
	if err != nil {
		return fmt.Errorf("models.init(fail to conntect database): %v", err)
	}

	// WARNNING: for serv command, MUST remove the output to os.stdout,
	// so use log file to instead print to stdout.
	execDir, _ := base.ExecDir()
	logPath := execDir + "/log/xorm.log"
	os.MkdirAll(path.Dir(logPath), os.ModePerm)

	f, err := os.Create(logPath)
	if err != nil {
		return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
	}
	orm.Logger = f

	orm.ShowSQL = true
	orm.ShowDebug = true
	orm.ShowErr = true
	return nil
}
開發者ID:popospectre,項目名稱:gogs,代碼行數:35,代碼來源:models.go

示例2: setEngine

func setEngine() {
	var err error
	switch DbCfg.Type {
	case "mysql":
		orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected]%s/%s?charset=utf8",
			DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
	case "postgres":
		orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
			DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
	case "sqlite3":
		os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
		orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
	default:
		fmt.Printf("Unknown database type: %s\n", DbCfg.Type)
		os.Exit(2)
	}
	if err != nil {
		fmt.Printf("models.init(fail to conntect database): %v\n", err)
		os.Exit(2)
	}

	// WARNNING: for serv command, MUST remove the output to os.stdout,
	// so use log file to instead print to stdout.

	//x.ShowDebug = true
	//orm.ShowErr = true
	f, err := os.Create("xorm.log")
	if err != nil {
		fmt.Printf("models.init(fail to create xorm.log): %v\n", err)
		os.Exit(2)
	}
	orm.Logger = f
	orm.ShowSQL = true
}
開發者ID:josephyzhou,項目名稱:gogs,代碼行數:34,代碼來源:models.go

示例3: XConDb

func XConDb() (*xorm.Engine, error) {
	switch {
	case dbtype == "sqlite":
		return xorm.NewEngine("sqlite3", DbName)

	case dbtype == "mysql":
		return xorm.NewEngine("mysql", "user=mysql [email protected]#9^&* dbname=mysql")

	case dbtype == "pgsql":
		return xorm.NewEngine("postgres", "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable")
	}
	return nil, errors.New("尚未設定數據庫連接")
}
開發者ID:JessonChan,項目名稱:toropress,代碼行數:13,代碼來源:models.go

示例4: ConDb

func ConDb() (*xorm.Engine, error) {
	switch {
	case dbtype == "sqlite":
		return xorm.NewEngine("sqlite3", "./data/sqlite.db")

	case dbtype == "mysql":
		return xorm.NewEngine("mysql", "user=mysql [email protected]#9^&* dbname=mysql")

	case dbtype == "pgsql":
		// "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable maxcons=10 persist=true"
		//return xorm.NewEngine("postgres", "host=110.76.39.205 user=postgres password=LhS88root dbname=pgsql sslmode=disable")
		return xorm.NewEngine("postgres", "user=postgres password=LhS88root dbname=mzr sslmode=disable")
	}
	return nil, errors.New("尚未設定數據庫連接")
}
開發者ID:rose312,項目名稱:mzr,代碼行數:15,代碼來源:mzs.go

示例5: SetDB

//設置數據庫
func SetDB() {
	path, _ := filepath.Abs("")
	c, _ := config.ReadDefault(fmt.Sprintf("%s/admin/conf/databases.conf", path))

	driver, _ := c.String("database", "db.driver")
	dbname, _ := c.String("database", "db.dbname")
	user, _ := c.String("database", "db.user")
	password, _ := c.String("database", "db.password")
	host, _ := c.String("database", "db.host")
	//prefix, _ := c.String("database", "db.prefix")

	//數據庫鏈接
	var err error
	Engine, err = xorm.NewEngine(driver, fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8", user, password, host, dbname))
	if err != nil {
		revel.WARN.Printf("錯誤: %v", err)
	}

	//緩存方式是存放到內存中,緩存struct的記錄數為1000條
	//cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
	//Engine.SetDefaultCacher(cacher)

	//控製台打印SQL語句
	//Engine.ShowSQL = true

	//控製台打印調試信息
	//Engine.ShowDebug = true

	//控製台打印錯誤信息
	//Engine.ShowErr = true

	//控製台打印警告信息
	//Engine.ShowWarn = true
}
開發者ID:qmdx,項目名稱:GoCMS,代碼行數:35,代碼來源:init.go

示例6: ConnectDb

func ConnectDb() (*xorm.Engine, error) {
	fmt.Println("database type: " + dbtype)
	switch {
	case dbtype == "sqlite":
		return xorm.NewEngine("sqlite3", dbname)

	case dbtype == "mysql":
		return xorm.NewEngine("mysql", fmt.Sprintf("%v:%[email protected](%v:%v)/%v?charset=%v",
			dbuser, dbpassword, dbhost, dbport, dbname, dbcharset))

	case dbtype == "pgsql":
		return xorm.NewEngine("postgres", fmt.Sprintf("%v:%[email protected](%v:%v)/%v?charset=%v",
			dbuser, dbpassword, dbhost, dbport, dbname, dbcharset))
	}
	return nil, errors.New("No database found!")
}
開發者ID:Raysmond,項目名稱:RaysGo,代碼行數:16,代碼來源:models.go

示例7: setEngine

func setEngine() {
	dbName := utils.Cfg.MustValue("db", "name")
	dbPwd := utils.Cfg.MustValue("db", "pwd_"+runtime.GOOS)

	if runtime.GOOS == "darwin" {
		u, err := user.Current()
		if err != nil {
			beego.Critical("models.init -> fail to get user:", err.Error())
			os.Exit(2)
		}
		dbPwd = utils.Cfg.MustValue("db", "pwd_"+runtime.GOOS+"_"+u.Username)
	}

	var err error
	x, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%[email protected]%v/%v?charset=utf8",
		utils.Cfg.MustValue("db", "user"), dbPwd,
		utils.Cfg.MustValue("db", "host"), dbName))
	if err != nil {
		beego.Critical("models.init -> fail to conntect database:", err.Error())
		os.Exit(2)
	}

	if beego.RunMode != "pro" {
		x.ShowDebug = true
		x.ShowErr = true
		//x.ShowSQL = true
	}

	beego.Trace("Initialized database ->", dbName)
}
開發者ID:juju2013,項目名稱:gowalker,代碼行數:30,代碼來源:models.go

示例8: ConDb

func ConDb() *xorm.Engine {

	/*
		engine, _ = xorm.NewEngine("sqlite3", "./test.db")
	*/
	engine, _ = xorm.NewEngine("postgres", "user=postgres [email protected]#$%^&* dbname=pgsql sslmode=disable")
	return engine
}
開發者ID:rose312,項目名稱:mzr,代碼行數:8,代碼來源:xo.go

示例9: InitDB

// should be call before pacakge called
func InitDB(driver, dataSource string) (err error) {
	x, err = xorm.NewEngine(driver, dataSource)
	if err != nil {
		return
	}
	// create tables
	return x.Sync(new(Project), new(File))
}
開發者ID:kaisne,項目名稱:gobuild,代碼行數:9,代碼來源:database.go

示例10: InitOrm

func InitOrm() *xorm.Engine {
	LoadDBConfig()
	orm, err := xorm.NewEngine("postgres", fmt.Sprintf("user=%s dbname=%s sslmode=%s",
		DBConfig.User, DBConfig.Name, DBConfig.SslMode))
	if err != nil {
		panic(err)
	}
	return orm
}
開發者ID:jartek,項目名稱:goapi,代碼行數:9,代碼來源:models.go

示例11: NewTestEngine

func NewTestEngine(x *xorm.Engine) (err error) {
	switch DbCfg.Type {
	case "mysql":
		x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8",
			DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
	case "postgres":
		x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
			DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
	// case "sqlite3":
	// 	os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
	// 	x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
	default:
		return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
	}
	if err != nil {
		return fmt.Errorf("models.init(fail to conntect database): %v", err)
	}

	return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
		new(Action), new(Access), new(Issue), new(Comment))
}
開發者ID:popospectre,項目名稱:gogs,代碼行數:21,代碼來源:models.go

示例12: SetupDb

func SetupDb() (engine *xorm.Engine) {
	var (
		err error
	)

	if engine, err = xorm.NewEngine("sqlite3", "/tmp/tmp.db"); err != nil {
		panic(err)
	}

	if err = engine.Sync(new(User)); err != nil {
		panic(err)
	}

	return engine
}
開發者ID:GoSteelProgrammers,項目名稱:talk-review-orms,代碼行數:15,代碼來源:query.go

示例13: init

func (s *ServerType) init() {

	s.DB, err = xorm.NewEngine("mysql", s.MYSQL_DSN)
	if err != nil {
		panic(err)
	}
	s.DB.ShowSQL = s.MYSQL_DEBUG

	if s.PHP_CLI == "" {
		s.PHP_CLI = DEFAULT_PHP_CLI
	}

	if s.LogFile != "" {
		logFile, err := os.OpenFile(s.LogFile, os.O_RDWR|os.O_CREATE, 0666)
		if err != nil {
			panic(err)
		}
		log.SetOutput(logFile)
	}

	//worker_num為0表示不啟用PHP引擎
	if s.PHP_WORKER_NUM != 0 {

		if s.PHP_TPL_DIR == "" {
			s.PHP_TPL_DIR = s.Root + "/static/template/"
		}

		//php模板引擎
		s.PHP = php.NewEngine(s.PHP_WORKER_NUM, s.PHP_CLI, s.PHP_TPL_DIR)
		//設置PHP的運行路徑
		s.PHP.RunDir = s.Root
		//初始化
		s.PHP.Init()

		go s.PHP.EngineLoop()
	}

	go Session_CheckExpire()

	//默認
	if s.DefaultAction == "" {
		s.DefaultAction = "index"
	}
	if s.DefaultController == "" {
		s.DefaultController = "Base"
	}
	s.Charset = "utf-8"
}
開發者ID:WaylandGod,項目名稱:easygo,代碼行數:48,代碼來源:server.go

示例14: init

func init() {
	st := NewSuite("xorm")
	st.InitF = func() {
		st.AddBenchmark("Insert", 2000*ORM_MULTI, XormInsert)
		st.AddBenchmark("MultiInsert 100 row", 500*ORM_MULTI, XormInsertMulti)
		st.AddBenchmark("Update", 2000*ORM_MULTI, XormUpdate)
		st.AddBenchmark("Read", 4000*ORM_MULTI, XormRead)
		st.AddBenchmark("MultiRead limit 100", 2000*ORM_MULTI, XormReadSlice)

		engine, _ := xorm.NewEngine("mysql", ORM_SOURCE)

		engine.SetMaxIdleConns(ORM_MAX_IDLE)
		engine.SetMaxConns(ORM_MAX_CONN)

		xo = engine.NewSession()
	}
}
開發者ID:KiraPro,項目名稱:orm-benchmark,代碼行數:17,代碼來源:xorm.go

示例15: init

func init() {
	var err error
	orm, err = xorm.NewEngine("sqlite3", "./test.db")
	if err != nil {
		fmt.Println(err)
	}

	orm.ShowSQL = true
	orm.ShowDebug = true

	err = orm.Sync(&User{}, &Repository{})
	if err != nil {
		fmt.Println(err)
	}

	base.RepoRootPath = "test"
}
開發者ID:Julianzz,項目名稱:gogs,代碼行數:17,代碼來源:models_test.go


注:本文中的github.com/lunny/xorm.NewEngine函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。