Exemplo n.º 1
0
func (p *Scope) PageByOrder(model T, order string, pf *filter.PageFilter) *db.Paging {
	p.checkModel(model)
	p.pf = pf
	p.haswhere = true
	p.Limit(pf.Skip(), pf.PerPage())
	w := p.buildWhere()
	psa := p.buildPageByOrder(order)
	sql_ := fmt.Sprintf("SELECT * %s %v %v", p.getFrom(), w.Sql, psa.Sql)
	var rows *sql.Rows
	paging := &db.Paging{}
	log.Println(sql_)
	if rows, p.Err = p._query(sql_, convertArgs(w)...); p.NotErr() {
		defer rows.Close()

		p.model.MapRowsAsLst(rows, model)
		paging.Total = p.Count(model)
		paging.Items = model
	}
	return paging
}
Exemplo n.º 2
0
func (p *Scope) PageJson2(lst T, pf *filter.PageFilter) *db.Paging {
	p.checkModel(lst)

	if pf != nil {
		p.builder.Limit(pf.PerPage(), pf.Skip())
		visitor := filter.Visitor{}
		visitor.Quote = p.orm.dialect.Quote
		sa := visitor.Visitor(pf.Group)
		p.builder.Where(sa.Sql, sa.Args...)
	}

	var rows *sql.Rows
	paging := &db.Paging{}
	paging.Total = p.Count(lst)

	if !p.builder.hasSelect() {
		p.builder.Select(`"Json"`)
	}
	sa := p.builder.SqlSelect()

	resultv := reflect.ValueOf(lst)
	if resultv.Kind() != reflect.Ptr || resultv.Elem().Kind() != reflect.Slice {
		panic("out argument must be a slice address")
	}
	slicev := resultv.Elem()
	if rows, p.Err = p._query2(sa); p.NotErr() {
		defer rows.Close()
		for rows.Next() {
			var v []byte
			rows.Scan(&v)
			obj := reflect.New(p.model.Type).Interface()
			json.Unmarshal(v, obj)
			slicev = reflect.Append(slicev, reflect.ValueOf(obj).Elem())
		}
		resultv.Elem().Set(slicev.Slice(0, slicev.Len()))
	}
	paging.Items = lst
	return paging
}
Exemplo n.º 3
0
func (p *Scope) Page2(model T, pf *filter.PageFilter) *db.Paging {
	p.checkModel(model)

	if pf != nil {
		p.builder.Limit(pf.PerPage(), pf.Skip())
		visitor := filter.Visitor{}
		visitor.Quote = p.orm.dialect.Quote
		sa := visitor.Visitor(pf.Group)
		p.builder.Where(sa.Sql, sa.Args...)
	}

	var rows *sql.Rows
	paging := &db.Paging{}
	paging.Total = p.Count(model)
	sa := p.builder.SqlSelect()
	if rows, p.Err = p._query2(sa); p.NotErr() {
		defer rows.Close()
		p.model.MapRowsAsLst(rows, model)
		paging.Items = model
	}
	return paging
}