func (r Rate) GetVotes(Db *gorp.DbMap) []Vote { if r.Id == 0 { return []Vote{} } Db.Select(&r.Votes, "select * from Vote where RateId = ?", r.Id) return r.Votes }
// Get list of available summoners func getSummoners(dbmap *gorp.DbMap) ( <-chan SummonerInfo, <-chan SummonerInfo) { summonerChan1 := make(chan SummonerInfo) summonerChan2 := make(chan SummonerInfo) // select summoners var summoners []SummonerInfo _, err := dbmap.Select( &summoners, "select id from summoners") checkErr(err, "Selecting summoner ids failed") globalWg.Add(1) go func() { for _, n := range summoners { summonerChan1 <- n summonerChan2 <- n } close(summonerChan1) close(summonerChan2) globalWg.Done() }() return summonerChan1, summonerChan2 }
func getAllCats(dbmap *gorp.DbMap) []interface{} { tab, err := dbmap.Select(Category{}, "SELECT * FROM Category") if err != nil { fmt.Println("Problem z Category InitDb") } return tab }
func PrintTable(dbMap *gorp.DbMap) { var users []User dbMap.Select(&users, "SELECT * FROM users") fmt.Printf("%+v\n", users) }
func (c *Article) AddReadReceipts(dbmap *gorp.DbMap) { var delivered []string _, err := dbmap.Select(&delivered, ` select readers.distinct_id from articles inner join read_receipts on read_receipts.article_id = articles.id inner join readers on read_receipts.reader_id = readers.id where articles.id = $1 and last_read_at > articles.updated_at`, c.Id) if err != nil { panic(err) } c.Delivered = delivered var pending []string _, err = dbmap.Select(&pending, ` select readers.distinct_id from articles inner join read_receipts on read_receipts.article_id = articles.id inner join readers on read_receipts.reader_id = readers.id where articles.id = $1 and (last_read_at is null or last_read_at < articles.updated_at)`, c.Id) if err != nil { panic(err) } c.Pending = pending }
func GetGeoObjects(db *gorp.DbMap, tp string, offset, limit int64) ([]GeoObject, error) { o := []GeoObject{} _, err := db.Select(&o, "select * from GeoObject where Type = ? limit ?,?", tp, offset, limit) return o, err }
func insertOrUpdateContact(dbmap *gorp.DbMap, user, contactid, contactname, group string) error { var contacts []Contact _, err := dbmap.Select(&contacts, "SELECT * FROM contacts WHERE User=? AND ContactId=?", user, contactid) if err != nil { return err } if len(contacts) == 0 { contact := Contact{ User: user, ContactId: contactid, ContactName: contactname, Group: group, } err = dbmap.Insert(&contact) if err != nil { return err } } else if len(contacts) == 1 { contact := contacts[0] contact.ContactName = contactname contact.Group = group _, err = dbmap.Update(&contact) if err != nil { return err } } else { return errors.New("You have more than one contacts") } return nil }
func (jail *Jail) Persist(db *gorp.DbMap) error { insert := false if len(jail.UUID) == 0 { insert = true count := 0 db.Select(&count, "select count(UUID) from Jail where Name = ?", jail.Name) if count > 0 { return fmt.Errorf("Jail with name %s already exists", jail.Name) } } if err := jail.Validate(); err != nil { return err } if insert { db.Insert(jail) } else { db.Update(jail) } for _, device := range jail.NetworkDevices { device.VmUUID = jail.UUID if err := device.Persist(db, jail); err != nil { return err } } for _, mount := range jail.Mounts { if mount.MountPointID == 0 { mount.JailUUID = jail.UUID db.Insert(mount) } else { db.Update(mount) } } for _, option := range jail.Options { if option.OptionID == 0 { option.JailUUID = jail.UUID db.Insert(option) } else { db.Update(option) } } for _, route := range jail.Routes { if route.RouteID == 0 { route.VmUUID = jail.UUID db.Insert(route) } else { db.Update(route) } } return nil }
func GetComments(db *gorp.DbMap, itemid int64, itemtype string) ([]Comment, error) { var cs []Comment _, err := db.Select(&cs, "select * from Comment where ItemId = ? and"+ " ItemType = ? and Deleted = 0 order by Id desc", itemid, itemtype) return cs, err }
func GetGoods(dbmap *gorp.DbMap) ([]GoodInfo, error) { var ginfo []GoodInfo _, err := dbmap.Select(&ginfo, "SELECT * FROM GOODS") if err != nil { glog.V(1).Infof("[DEBUG:] Search goods fail:%v", err) return ginfo, err } return ginfo, nil }
func GetAllNews(db *gorp.DbMap, offset, count int64) ([]New, error) { n := []New{} _, err := db.Select(&n, "select * from New order by Id desc limit ?,?", offset, count) if err != nil { return n, err } return n, nil }
func Search(db *gorp.DbMap, q, itemtype string, offset, limit int64) ([]SearchIndex, error) { result := []SearchIndex{} q = strings.ToLower(q) _, err := db.Select(&result, "select * from SearchIndex where Keys like ?"+ " and Type = ? group by Type, ItemId order by Weight limit ?,?", "%"+q+"%", itemtype, offset, limit) return result, err }
func GetItems(db *gorp.DbMap, tagId int64, itemType string) []int64 { var r = []int64{} _, err := db.Select(&r, "select ItemId from Tags where Id = ? and ItemType = ?", tagId, itemType) if err != nil { fmt.Println(err) } return r }
func UnreadArticles(dbmap *gorp.DbMap, readerId int64) (keys []Article, err error) { _, err = dbmap.Select(&keys, ` select articles.*, first_read_at, last_read_at, read_count from articles inner join read_receipts on read_receipts.article_id = articles.id where (first_read_at is null or articles.updated_at < read_receipts.last_read_at) and read_receipts.reader_id = $1`, readerId) return }
func GetByUuid(db *gorp.DbMap, uuid string) (*User, error) { u, err := db.Select(User{}, "select * from User where Uuid = ?", uuid) if err != nil { return nil, err } if len(u) == 0 { return nil, errors.New("User not found") } return u[0].(*User), nil }
func ListTasks(r render.Render, db *gorp.DbMap, log *log.Logger) { var taskIds []tasksView _, err := db.Select(&taskIds, "select id,name from tasks order by id") if err != nil { log.Printf("Error selecting from database: %v", err) r.JSON(500, map[string]string{"message": "error while retrieving tasks"}) return } r.JSON(200, taskIds) }
/* return array of all Entries */ func All(dbMap *gorp.DbMap) []*Entry { var entries []*Entry _, err := dbMap.Select(&entries, "SELECT * FROM entries") if err != nil { panic(err) } return entries }
func GetNewBySlug(db *gorp.DbMap, id string) (New, error) { var nws = []New{} _, err := db.Select(&nws, "select * from New where Slug = ? limit 1", id) if err != nil { return New{}, err } if len(nws) == 1 { return nws[0], nil } return New{}, errors.New("Not found") }
func GetPage(db *gorp.DbMap, slug string) (*Page, error) { ps := []Page{} _, err := db.Select(&ps, "select * from Page where Slug = ? ", slug) if err != nil { return nil, err } if len(ps) == 1 { return &ps[0], nil } return nil, errors.New("Not found") }
func GetNetworks(db *gorp.DbMap) []Network { var networks []Network _, err := db.Select(&networks, "select * from Network") if err != nil { panic(err) return []Network{} } return networks }
func PostersGetMonth(db *gorp.DbMap, t int64) (Posters, error) { tm := unixtime.Parse(t).In(time.Now().Location()) startMonth := now.New(tm).BeginningOfMonth().UnixNano() startMonth -= 24 * 60 * 60 endMonth := now.New(tm).EndOfMonth().UnixNano() endMonth += 24 * 60 * 60 var p Posters _, err := db.Select(&p, "select * from Poster where StartDate > ? and"+ " StartDate < ? and Deleted = 0 and Published = 1 order by StartDate", startMonth, endMonth) return p, err }
func FewGet(db *gorp.DbMap, offset, limit int64) ([]ChangeLog, error) { var res []ChangeLog _, err := db.Select(&res, "select * from ChangeLog order by Id desc limit"+ " ?,?", offset, limit) if err != nil { return []ChangeLog{}, err } for i, v := range res { res[i].Rate = v.Rate.GetRate(db, 6, v.Id) } return res, err }
func GetOrders(db *gorp.DbMap, userid, storeid int64) ([]Order, error) { var o = []Order{} _, err := db.Select(&o, "select * from 'Order'") if err != nil { return o, err } for i, v := range o { o[i].Products, err = GetOrderProducts(db, v.Id) if err != nil { return o, err } } return o, err }
func GetAllJails(db *gorp.DbMap) []*Jail { var uuids []string var jails []*Jail if _, err := db.Select(&uuids, "select UUID from Jail"); err != nil { return nil } for _, uuid := range uuids { jails = append(jails, GetJail(db, map[string]interface{}{"uuid": uuid})) } return jails }
/* return the latest */ func Latest(dbMap *gorp.DbMap) *Entry { // find most recent entry var entries []*Entry _, err := dbMap.Select(&entries, "SELECT * FROM entries ORDER BY start_time DESC LIMIT 1") if err != nil { panic(err) } if len(entries) > 0 { return entries[0] } return nil }
func getNews(db *gorp.DbMap, offset, count int64, admin bool) ([]New, error) { n := []New{} var adm = "" if !admin { adm = " and Published = 1" } _, err := db.Select(&n, "select * from New where Deleted = 0 "+adm+" order by "+ "Id desc limit ?,?", offset, count) if err != nil { return n, err } for i, v := range n { n[i].Rate = v.Rate.GetRate(db, 4, v.Id) } return n, nil }
func main() { var ( dbMap *gorp.DbMap ) dbMap = SetupDb() //START SETUP OMIT dbMap.Insert(&User{ Id: 1, FirstName: "John", LastName: "Doe", Age: 24, }) dbMap.Insert(&User{ Id: 2, FirstName: "Jane", LastName: "Doe", Age: 52, }) dbMap.Insert(&User{ Id: 3, FirstName: "Joe", LastName: "Shmoe", Age: 10, }) //END SETUP OMIT //START CODE OMIT var users []User dbMap.Select(&users, "SELECT * FROM users WHERE last_name = ? OR age < ? ORDER BY age DESC LIMIT 2", "Doe", 12) fmt.Printf("%+v\n", users) //END CODE OMIT }
func GetPosts(args martini.Params, r render.Render, dbmap *gorp.DbMap, req *http.Request) { var posts []models.Post page, perPage := HandlePageParams(req) postsSql := &models.AllPosts{Page: page, PerPage: perPage} _, err := dbmap.Select(&posts, postsSql.GetAllPosts()) log.Printf("%#v", posts) if err != nil { log.Fatal("Something went wrong") jsonError := &models.JSONError{ Error: "Something went wrong", } r.JSON(500, jsonError) } else { r.JSON(200, posts) } }
func ProductsIndex(db *gorp.DbMap, params martini.Params, render render.Render, request *http.Request) { query := request.URL.Query() var limit, offset string //// There are no ORM chain methods for querying if query.Get("limit") != "" { limit = " LIMIT " + query.Get("limit") } if query.Get("offset") != "" { offset = " OFFSET " + query.Get("offset") } var products []models.Product _, err := db.Select(&products, "SELECT * FROM products"+limit+offset) if err == nil { render.JSON(200, products) } else { render.JSON(404, "") } }
func InitDb(dbmap *gorp.DbMap) { tab, err := dbmap.Select(Category{}, "SELECT * FROM Category") if err != nil { fmt.Println("Problem z Category InitDb") } count := len(tab) categories = make([]*Category, count) var max int for key, row := range tab { cat := row.(*Category) categories[key] = cat if max < cat.Count { max = cat.Count } } for _, cat := range categories { cat.FontSize = getFontSize(max, cat.Count) } }