func (m *topicModel) FindById(id int64) *Topic { var t *Topic rows, e := orm.NewStmt().Select("t.*").From("Topic", "t"). Where("t.id = ?").Query(id) if e == nil { rows.ScanRow(&t) } return t }
func (m *roleModel) RoleByName(name string) *Role { var r *Role rows, e := orm.NewStmt("").Select("r.*"). From("Role", "r").Where("r.name = ?").Query(name) if e == nil { rows.ScanRow(&r) } return r }
func (m *userRoleModel) Save(u *User, r *Role) bool { _, e := orm.NewStmt(""). Insert("UserRole", "user_id", "role_id", "created_at"). Exec(u.Id, r.Id, time.Now()) if e != nil { log.Println(e) return false } return true }
func (m *userModel) User(id int64) *User { var u *User rows, e := orm.NewStmt("").Select("u.*"). From("User", "u").Where("u.id = ?").Query(id) if e == nil { rows.ScanRow(&u) } return u }
func (m *userModel) FindByEmail(email string) *User { var u *User rows, e := orm.NewStmt().Select("u.*"). From("User", "u").Where("u.email = ?").Query(email) if e == nil { rows.ScanRow(&u) } return u }
func baseQueryStmt() *orm.Stmt { return orm.NewStmt(""). Select("b.*, l.*, c.*, a.*, r.*, s.*, t.*, o.*"). From("Base", "b"). InnerJoin("Location", "l", "l.x = b.x AND l.y = b.y"). LeftJoin("Center", "c", "c.base_id = b.id"). LeftJoin("Lab", "a", "a.base_id = b.id"). LeftJoin("Armory", "r", "r.base_id = b.id"). LeftJoin("Supply", "s", "s.base_id = b.id"). LeftJoin("Stope", "t", "t.base_id = b.id"). LeftJoin("Storage", "o", "o.base_id = o.id") }
func (m *mapModel) SaveResource(loc *Location) bool { _, e := orm.NewStmt(""). Update("Location", "l", "metal", "energy", "refresh_at"). Where("l.x = ? AND l.y = ?"). Exec(loc.Metal, loc.Energy, loc.RefreshAt, loc.X, loc.Y) if e != nil { log.Println(e) return false } return true }
func (m *userModel) UserByEmail(email string) *User { var u *User rows, e := orm.NewStmt("").Select("u.*"). From("User", "u").Where("u.email = ?").Query(email) if e != nil { log.Println(e) return nil } rows.ScanRow(&u) return u }
func (m *userRoleModel) Remove(u *User, r *Role) bool { _, e := orm.NewStmt(""). Delete("UserRole"). Where("user_id = ? AND role_id = ?"). Exec(u.Id, r.Id) if e != nil { log.Println(e) return false } return true }
func (loc *Location) Base() *Base { if loc.base == nil { rows, e := orm.NewStmt(""). Select("b.*"). From("Base", "b"). Where("b.x = ? AND b.y = ?"). Query(loc.X, loc.Y) if e == nil { rows.ScanRow(&loc.base) } } return loc.base }
func (m *mapModel) refresh() { for now := range time.Tick(MapRefreshInterval) { if m.RefreshState == RefreshStateOff { continue } rows, e := orm.NewStmt(""). Select("l.*").From("Location", "l"). Where("l.base_id = 0"). Asc("l.refresh_at").Asc("l.x").Asc("l.y"). Limit(10).Query() if e != nil { log.Println(e) continue } for rows.Next() { var loc *Location if rows.ScanEntity(&loc) != nil { continue } metal, energy := loc.Metal, loc.Energy if rand.Intn(100) < ResourceRate { n := rand.Intn(100) total := int64(ResourceMin + (ResourceMax-ResourceMin)*n/100) if rand.Intn(100) < EnergyRate { loc.Energy = total * (int64(rand.Intn(10)) + 1) / 10 } else { loc.Energy = 0 } loc.Metal = total - loc.Energy } else { loc.Metal = 0 loc.Energy = 0 } m.IncrMetal(loc.Metal - metal) m.IncrEnergy(loc.Energy - energy) loc.RefreshAt = now m.SaveResource(loc) } } }
func (m *commentModel) FindBy(k string, v interface{}) []*Comment { stmt := orm.NewStmt().Select("c.*").From("Comment", "c"). Where(fmt.Sprintf("`c`.`%s` = ?", k)).Asc("id") rows, e := stmt.Query(v) if e != nil { return nil } comments := make([]*Comment, 0) for rows.Next() { var c *Comment rows.ScanEntity(&c) comments = append(comments, c) } return comments }
func (m *mapModel) Location(x, y int) *Location { var loc *Location var base *Base rows, e := orm.NewStmt(""). Select("l.*,b.*"). From("Location", "l"). LeftJoin("Base", "b", "b.x = l.x AND b.y = l.y"). Where("l.x = ? AND l.y = ?"). Query(x, y) if e != nil { log.Println(e) return nil } rows.ScanRow(&loc, &base) if base.Id > 0 { loc.base = base } return loc }
func (u *User) Roles() map[string]*Role { if u.roles == nil { rows, e := orm.NewStmt(""). Select("r.*").From("Role", "r"). InnerJoin("UserRole", "ur", "r.id = ur.role_id"). Where("ur.user_id = ?"). Query(u.Id) if e != nil { log.Println(e) return nil } u.roles = make(map[string]*Role) for rows.Next() { var r *Role rows.ScanEntity(&r) u.roles[r.Name] = r } } return u.roles }
func (m *mapModel) Rect(x, y, w, h int) []*Location { rows, e := orm.NewStmt(""). Select("l.*").From("Location", "l"). Where("l.x BETWEEN ? AND ? AND l.y BETWEEN ? AND ?"). Query(x, x+w, y, y+h) locs := make([]*Location, 0) if e != nil { log.Println(e) return locs } for rows.Next() { var loc *Location rows.ScanEntity(&loc) locs = append(locs, loc) } return locs }
func (u *User) Bases() map[int64]*Base { if u.bases == nil { rows, e := orm.NewStmt(""). Select("b.*"). From("Base", "b"). Where("b.user_id = ?"). Query(u.Id) if e != nil { log.Println(e) return nil } u.bases = make(map[int64]*Base) for rows.Next() { var b *Base rows.ScanEntity(&b) u.bases[b.Id] = b } } return u.bases }
func (m *topicModel) Search(k, v string) []*Topic { stmt := orm.NewStmt().Select("t.*").From("Topic", "t"). Desc("id").Where(fmt.Sprintf( "`t`.`%s` LIKE ? AND `t`.`state` = ?", k)) rows, e := stmt.Query("%"+v+"%", TopicStatePublished) if e != nil { log.Println(e) return nil } topics := make([]*Topic, 0) for rows.Next() { var t *Topic rows.ScanEntity(&t) topics = append(topics, t) } return topics }
func (m *userModel) Search(key string, order map[string]string, limit, offset int64) []*User { key = "%" + key + "%" stmt := orm.NewStmt(""). Select("u.*").From("User", "u"). Where("u.name like ? or u.email like ?"). Limit(limit). Offset(offset) for k, v := range order { stmt.OrderBy("u."+k, v) } rows, e := stmt.Query(key, key) if e != nil { log.Println(e) return nil } users := make([]*User, 0, limit) for rows.Next() { var u *User rows.ScanEntity(&u) users = append(users, u) } return users }
func (m *userModel) Exists(email string) bool { n, _ := orm.NewStmt("").Count("User", "u"). Where("u.email = ?").Exec(email) return n > 0 }