Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for OrderPrepend #61

Closed
arizanovj opened this issue Apr 18, 2018 · 0 comments
Closed

Support for OrderPrepend #61

arizanovj opened this issue Apr 18, 2018 · 0 comments

Comments

@arizanovj
Copy link

arizanovj commented Apr 18, 2018

Hi.
First of all thanks for the library. It's quite handy for building complex queries.
I have a use case that I can't produce. I am building keyset pagination and what I need to achieve is the following:
In the models where I need the pagination I pass the pagination struct. So for example in the product model I have this:

func (p *Product) GetProducts(pagination *pagination.Pagination) *[]Product {
	var (
		product  Product
		products []Product
	)
	pagination.PK = "id"
	query := p.Env.QB.From("product").Order(goqu.I("name").Asc()).Prepared(true)

	query = pagination.Paginate(query)
	sql, args, _ := query.ToSql()
}

And this is the the pagination struct which needs to "paginate" the query.

type Pagination struct {
	ID        *uint64 `json:"ID" schema:"ID"`
	PerPage   int64   `json:"perPage" schema:"perPage"`
	Direction string  `json:"direction" schema:"direction"`
	PK        string
	Env       *env.Env
}
func (p *Pagination) Paginate(query *goqu.Dataset) *goqu.Dataset {
	order := query.GetClauses().Order

	if order != nil {
		query.ClearOrder()
	}
	query = query.Order(p.GetOrder())
	if order != nil {
		fmt.Printf("%+v\n", order)
		//orderedExpression := goqu.OrderedExpression{}
		//query = query.GetClauses().Order.Append(order)

		//query = query.OrderAppend(order...)
	}

	if p.ID != nil {
		fmt.Printf("%+v\n", order)
		query = query.Where(p.GetWhere())
	}

	query = query.Limit(uint(p.PerPage))

	if p.Direction == "down" {
		query = p.Invert(query)
	}

	return query
}

func (p *Pagination) GetOrder() goqu.OrderedExpression {
	if p.Direction == "up" {
		return goqu.I(p.PK).Asc()
	} else {
		return goqu.I(p.PK).Desc()
	}
}

So in the paginate function I have this piece of code.

order := query.GetClauses().Order

	if order != nil {
		query.ClearOrder()
	}
	query = query.Order(p.GetOrder())
	if order != nil {
		fmt.Printf("%+v\n", order)
		
		//query = query.GetClauses().Order.Append(order)

		//query = query.OrderAppend(order...)
   }

What I am trying to do is extract the previously set order, add the new order to the *Dataset and append everything else that was previously added. I need this since the order for the primary ID needs to get precedence over the orders of all other columns. Basically what I need is OrderPrepend function which I can't find in the library.

I think I need the following function exposed as public to achieve that. What do you suggest ?

func orderList(vals ...OrderedExpression) ColumnList {..}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants