func init() { dataSource := conf.Config.Datasource maxIdleConns := conf.Config.MaxIdleConns maxOpenConns := conf.Config.MaxOpenConns log.Println("mysql datasource:", dataSource, ", maxIdleConns", maxIdleConns, ", maxOpenConns", maxOpenConns) db, err := sql.Open("mysql", dataSource) utils.HandleError(err) db.SetMaxIdleConns(maxIdleConns) db.SetMaxOpenConns(maxOpenConns) DbMap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}} // Trace query log if conf.IsDev() { DbMap.TraceOn("", log.New(os.Stdout, "", log.Lmicroseconds)) } // Define table metadata new(UserManager).AddTable() new(ChatroomManager).AddTable() utils.HandleError(DbMap.CreateTablesIfNotExists()) }
func init() { Config = &AppConfig{} jsonString, err := ioutil.ReadFile("./conf/conf.json") utils.HandleError(err) err = json.Unmarshal(jsonString, &Config) utils.HandleError(err) Runmode = &RunmodeEnums{DEV: &utils.StringEnum{Value: "dev"}, PROD: &utils.StringEnum{Value: "prod"}} }
func (this *ChatroomManager) Find(pagination *Pagination) []*Chatroom { var chatrooms []*Chatroom _, err := DbMap.Select(&chatrooms, "SELECT * FROM "+this.TableName()+" LIMIT ?, ?", (pagination.Page-1)*pagination.Length, pagination.Length) utils.HandleError(err) return chatrooms }
func (this *UserManager) Find(t int, pagination *Pagination) []*User { var users []*User _, err := DbMap.Select(&users, "SELECT * FROM "+this.TableName()+" WHERE type = ? LIMIT ?, ?", t, (pagination.Page-1)*pagination.Length, pagination.Length) utils.HandleError(err) return users }
func (this *Cache) Increment(key string, delta uint64) uint64 { newValue, err := this.client.Increment(key, delta) utils.HandleError(err) if conf.IsDev() { log.Println("cache increment:", key, delta) } return newValue }
func (this *Cache) Get(key string) []byte { item, err := this.client.Get(key) if conf.IsDev() { utils.HandleError(err) } if item == nil { if conf.IsDev() { log.Println("cache not hit:", key) } return nil } if conf.IsDev() { log.Println("cache hit:", key) } return item.Value }
func (this *ChatroomManager) Create(chatroom *Chatroom) *Chatroom { utils.HandleError(DbMap.Insert(chatroom)) // modules.AppCache.Delete(this.CountCacheKey()) return chatroom }
func (this *ChatroomManager) Count() int64 { count, err := DbMap.SelectInt("SELECT cnt FROM table_stats WHERE table_name = ?", this.TableName()) utils.HandleError(err) return count }
func (this *UserManager) Create(user *User) *User { utils.HandleError(DbMap.Insert(user)) return user }
func (this *UserManager) Count(t int) int64 { count, err := DbMap.SelectInt("SELECT count(*) FROM "+this.TableName()+" WHERE type = ?", t) utils.HandleError(err) return count }