Example #1
0
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())
}
Example #2
0
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"}}
}
Example #3
0
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
}
Example #4
0
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
}
Example #5
0
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
}
Example #6
0
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
}
Example #7
0
func (this *ChatroomManager) Create(chatroom *Chatroom) *Chatroom {
	utils.HandleError(DbMap.Insert(chatroom))
	// modules.AppCache.Delete(this.CountCacheKey())

	return chatroom
}
Example #8
0
func (this *ChatroomManager) Count() int64 {
	count, err := DbMap.SelectInt("SELECT cnt FROM table_stats WHERE table_name = ?", this.TableName())
	utils.HandleError(err)

	return count
}
Example #9
0
func (this *UserManager) Create(user *User) *User {
	utils.HandleError(DbMap.Insert(user))
	return user
}
Example #10
0
func (this *UserManager) Count(t int) int64 {
	count, err := DbMap.SelectInt("SELECT count(*) FROM "+this.TableName()+" WHERE type = ?", t)
	utils.HandleError(err)

	return count
}