Example #1
0
// Returns the first row in the table that matches certain conditions.
func (t *MysqlTable) Find(terms ...interface{}) db.Item {

	var item db.Item

	terms = append(terms, db.Limit(1))

	result := t.invoke("FindAll", terms)

	if len(result) > 0 {
		response := result[0].Interface().([]db.Item)
		if len(response) > 0 {
			item = response[0]
		}
	}

	return item
}
Example #2
0
// Returns the first db.Item that matches the given conditions.
func (self *SourceCollection) Find(terms ...interface{}) db.Item {

	var item db.Item

	terms = append(terms, db.Limit(1))

	result := self.invoke("FindAll", terms)

	if len(result) > 0 {
		response := result[0].Interface().([]db.Item)
		if len(response) > 0 {
			item = response[0]
		}
	}

	return item
}
Example #3
0
// Creates a pager from a db.Collection.
func Pager(collection db.Collection, conds db.Cond, page int) sugar.Map {

	total, _ := collection.Count(conds)

	// Calculating total pages
	pages := int(math.Ceil(float64(total) / float64(ItemsPerPage)))

	// Getting current page
	if page < 1 {
		page = 1
	}

	prev := page - 1

	next := page + 1

	if next > total {
		next = 0
	}

	data := collection.FindAll(
		conds,
		db.Offset((page-1)*ItemsPerPage),
		db.Limit(ItemsPerPage),
	)

	response := sugar.Map{
		"total": total,
		"pager": sugar.Map{
			"count":   pages,
			"size":    ItemsPerPage,
			"next":    next,
			"prev":    prev,
			"current": page,
		},
		"data": data,
	}

	return response
}