func main() { fmt.Println("-----start sqlite go routines-----") engine, err := sqliteEngine() if err != nil { fmt.Println(err) return } engine.ShowSQL = true cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) engine.SetDefaultCacher(cacher) fmt.Println(engine) test(engine) fmt.Println("test end") engine.Close() fmt.Println("-----start mysql go routines-----") engine, err = mysqlEngine() engine.ShowSQL = true cacher = xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) engine.SetDefaultCacher(cacher) if err != nil { fmt.Println(err) return } defer engine.Close() test(engine) }
func InitEngine() (*xorm.Engine, error) { var err error Engine, err = ConnectDb() cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Engine.SetDefaultCacher(cacher) return Engine, err }
func SetEngine() (*xorm.Engine, error) { var err error Engine, err = XConDb() //Engine.Mapper = xorm.SameMapper{} //Engine.SetMaxConns(5) //Engine.ShowSQL = true cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Engine.SetDefaultCacher(cacher) return Engine, err }
func SetEngine() (*xorm.Engine, error) { var err error Engine, err = ConDb() //Engine.Mapper = xorm.SameMapper{} //Engine.SetMaxConns(5) //Engine.ShowSQL = true cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Engine.SetDefaultCacher(cacher) Engine.ShowDebug = true Engine.ShowErr = true Engine.ShowSQL = true f, err := os.Create("./xsql.log") if err != nil { println(err.Error()) panic("OMG!") } Engine.Logger = f return Engine, err }
//设置数据库 func SetDB() { path, _ := filepath.Abs("") c, _ := config.ReadDefault(fmt.Sprintf("%s/src/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:%s@tcp(%s)/%s?charset=utf8", user, password, host, dbname)) if err != nil { revel.WARN.Printf("错误: %v", err) } cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Engine.SetDefaultCacher(cacher) //控制台打印SQL语句 //Engine.ShowSQL = true }
func main() { //runtime.GOMAXPROCS(2) // load config var err error cfg, err := config.Load("config.ini") if err != nil { fmt.Println(err) return } cfgs := cfg.Map() // create Orm var orm *xorm.Engine orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8", cfgs["dbuser"], cfgs["dbpasswd"], cfgs["dbhost"], cfgs["dbname"])) if err != nil { fmt.Println(err) return } orm.ShowSQL, _ = cfg.GetBool("showSql") orm.ShowDebug, _ = cfg.GetBool("showDebug") err = orm.Sync(&User{}, &Question{}, &QuestionFollow{}, &UserFollow{}, &Answer{}, &AnswerUp{}, &QuestionComment{}, &AnswerComment{}, &Tag{}, &QuestionTag{}, &Message{}, &Topic{}, &QuestionTopic{}, &TopicFollow{}, &News{}) if err != nil { fmt.Println(err) return } server := xweb.MainServer() app := xweb.RootApp() app.SetConfig("Orm", orm) if useCache, _ := cfg.GetBool("useCache"); useCache { server.Info("using orm cache system ...") cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) orm.SetDefaultCacher(cacher) } // add actions xweb.AddAction(&HomeAction{}) xweb.AutoAction(&ExerciseAction{}, &QuestionAction{}, &NewsAction{}) xweb.AddAction(&UserAction{}) // add login filter loginFilter := xweb.NewLoginFilter(app, USER_ID_TAG, "/login") loginFilter.AddAnonymousUrls("/", "/exercise", "/exercise/compile", "/news", "/login", "/about", "/register") app.AddFilter(loginFilter) // add func or var app scope app.AddTmplVar("AppVer", func() string { return "v" + APP_VER }) // run the web server xweb.Run(fmt.Sprintf("%v:%v", cfgs["address"], cfgs["port"])) }
func main() { f := "cache.db" os.Remove(f) Orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return } Orm.ShowSQL = true cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Orm.SetDefaultCacher(cacher) err = Orm.CreateTables(&User{}) if err != nil { fmt.Println(err) return } _, err = Orm.Insert(&User{Name: "xlw"}) if err != nil { fmt.Println(err) return } users := make([]User, 0) err = Orm.Find(&users) if err != nil { fmt.Println(err) return } fmt.Println("users:", users) users2 := make([]User, 0) err = Orm.Find(&users2) if err != nil { fmt.Println(err) return } fmt.Println("users2:", users2) users3 := make([]User, 0) err = Orm.Find(&users3) if err != nil { fmt.Println(err) return } fmt.Println("users3:", users3) user4 := new(User) has, err := Orm.Id(1).Get(user4) if err != nil { fmt.Println(err) return } fmt.Println("user4:", has, user4) user4.Name = "xiaolunwen" _, err = Orm.Id(1).Update(user4) if err != nil { fmt.Println(err) return } fmt.Println("user4:", user4) user5 := new(User) has, err = Orm.Id(1).Get(user5) if err != nil { fmt.Println(err) return } fmt.Println("user5:", has, user5) _, err = Orm.Id(1).Delete(new(User)) if err != nil { fmt.Println(err) return } for { user6 := new(User) has, err = Orm.Id(1).Get(user6) if err != nil { fmt.Println(err) return } fmt.Println("user6:", has, user6) } }